4
4
Writing documentation
5
5
=====================
6
6
7
+ .. contents :: Contents
8
+ :depth: 2
9
+ :local:
10
+ :backlinks: top
11
+ :class: multicol-toc
12
+
13
+
7
14
Getting started
8
15
===============
9
16
@@ -213,38 +220,52 @@ is better than:
213
220
In addition, since underscores are widely used by Sphinx itself, use
214
221
hyphens to separate words.
215
222
223
+ .. _referring-to-other-code :
216
224
217
225
Referring to other code
218
226
-----------------------
219
227
220
228
To link to other methods, classes, or modules in Matplotlib you can use
221
229
back ticks, for example:
222
230
223
- .. code-block :: python
231
+ .. code-block :: rst
224
232
225
- `~ matplotlib.collections.LineCollection`
233
+ `matplotlib.collections.LineCollection`
226
234
227
- returns a link to the documentation of
228
- `~matplotlib.collections.LineCollection `. For the full path of the
229
- class to be shown, omit the tilde:
235
+ generates a link like this: `matplotlib.collections.LineCollection `.
230
236
231
- .. code-block :: python
237
+ *Note: * We use the sphinx setting `default_role = 'obj' ` so that you don't
238
+ have to use qualifiers like `:class: `, `:func: `, `:meth: ` and the likes.
232
239
233
- `matplotlib.collections.LineCollection`
240
+ Often, you don't want to show the full package and module name. As long as the
241
+ target is unanbigous you can simply leave them out:
242
+
243
+ .. code-block :: rst
234
244
235
- to get `matplotlib.collections.LineCollection `. It is often not
236
- necessary to fully specify the class hierarchy unless there is a namespace
237
- collision between two packages:
245
+ `.LineCollection`
238
246
239
- .. code-block :: python
247
+ and the link still works: `.LineCollection `.
248
+
249
+ If there are multiple code elements with the same name (e.g. ``plot() `` is a
250
+ method in multiple classes), you'll have to extend the definition:
251
+
252
+ .. code-block :: rst
253
+
254
+ `.pyplot.plot` or `.Axes.plot`
255
+
256
+ These will show up as `.pyplot.plot ` or `.Axes.plot `. To still show only the
257
+ last segment you can add a tilde as prefix:
258
+
259
+ .. code-block :: rst
240
260
241
- `~ .LineCollection `
261
+ `~.pyplot.plot` or `~.Axes.plot `
242
262
243
- links just as well: `~.LineCollection `.
263
+ will render as ` ~.pyplot.plot ` or `~.Axes.plot `.
244
264
245
- Other packages can also be linked via ``intersphinx ``:
265
+ Other packages can also be linked via
266
+ `intersphinx <http://www.sphinx-doc.org/en/master/ext/intersphinx.html >`_:
246
267
247
- .. code-block :: Python
268
+ .. code-block :: rst
248
269
249
270
`numpy.mean`
250
271
@@ -296,13 +317,19 @@ when the documentation is built.
296
317
Writing docstrings
297
318
==================
298
319
299
- Much of the documentation lives in "docstrings". These are comment blocks
300
- in source code that explain how the code works. All new or edited docstrings
301
- should conform to the numpydoc guidelines. These split the docstring into a
302
- number of sections - see the `numpy documentation howto `_
303
- for more details and a guide to how docstrings should be formatted. Much of
304
- the ReST _ syntax discussed above (:ref: writing-rest-pages) can be used for
305
- links and references. These docstrings eventually populate the
320
+ Most of the API documentation is written in docstrings. These are comment
321
+ blocks in source code that explain how the code works.
322
+
323
+ .. note ::
324
+
325
+ Some parts of the documentation do not yet conform to the current
326
+ documentation style. If in doubt, follow the rules given here and not what
327
+ you may see in the source code. Pull requests updating docstrings to
328
+ the current style are very welcome.
329
+
330
+ All new or edited docstrings should conform to the `numpydoc docstring guide `_.
331
+ Much of the ReST _ syntax discussed above (:ref: `writing-rest-pages `) can be
332
+ used for links and references. These docstrings eventually populate the
306
333
:file: `doc/api ` directory and form the reference documentation for the
307
334
library.
308
335
@@ -313,21 +340,21 @@ An example docstring looks like:
313
340
314
341
.. code-block :: python
315
342
316
- def hlines (self , y , xmin , xmax , colors = ' k' , linestyles = ' solid' ,
317
- label = ' ' , ** kwargs ):
343
+ def hlines (self , y , xmin , xmax , colors = ' k' , linestyles = ' solid' ,
344
+ label = ' ' , ** kwargs ):
318
345
"""
319
346
Plot horizontal lines at each *y* from *xmin* to *xmax*.
320
347
321
348
Parameters
322
349
----------
323
- y : scalar or sequence of scalar
350
+ y : float or array-like
324
351
y-indexes where to plot the lines.
325
352
326
- xmin, xmax : scalar or 1D array_like
353
+ xmin, xmax : float or array-like
327
354
Respective beginning and end of each line. If scalars are
328
- provided, all lines will have same length.
355
+ provided, all lines will have the same length.
329
356
330
- colors : array_like of colors, optional, default: 'k'
357
+ colors : array-like of colors, optional, default: 'k'
331
358
332
359
linestyles : {'solid', 'dashed', 'dashdot', 'dotted'}, optional
333
360
@@ -352,104 +379,137 @@ See the `~.Axes.hlines` documentation for how this renders.
352
379
The Sphinx _ website also contains plenty of documentation _ concerning ReST
353
380
markup and working with Sphinx in general.
354
381
355
- .. note ::
356
-
357
- Some parts of the documentation do not yet conform to the current
358
- documentation style. If in doubt, follow the rules given here and not what
359
- you may see in the source code. Pull requests updating docstrings to
360
- the current style are very welcome.
361
-
362
382
Formatting conventions
363
383
----------------------
364
384
365
- The basic docstring conventions are covered in the `numpy documentation howto `_
385
+ The basic docstring conventions are covered in the `numpydoc docstring guide `_
366
386
and the Sphinx _ documentation. Some Matplotlib-specific formatting conventions
367
387
to keep in mind:
368
388
369
- * Matplotlib does not have a convention whether to use single-quotes or
370
- double-quotes. There is a mixture of both in the current code.
389
+ Function arguments
390
+ Function arguments and keywords within docstrings should be referred to
391
+ using the ``*emphasis* `` role. This will keep Matplotlib's documentation
392
+ consistent with Python's documentation:
371
393
372
- * Long parameter lists should be wrapped using a ``\ `` for continuation and
373
- starting on the new line without any indent:
394
+ .. code-block :: rst
374
395
375
- .. code-block :: python
396
+ If *linestyles* is *None*, the 'solid' is used.
376
397
377
- def add_axes (self , * args , ** kwargs ):
378
- """
379
- ...
398
+ Do not use the ```default role` `` or the ````literal`` `` role:
380
399
381
- Parameters
382
- ----------
383
- projection :
384
- {'aitoff', 'hammer', 'lambert', 'mollweide', 'polar', \
385
- 'rectilinear'}, optional
386
- The projection type of the axes.
400
+ .. code-block :: rst
387
401
388
- ...
389
- """
402
+ Neither `argument` nor ``argument`` should be used.
390
403
391
- Alternatively, you can describe the valid parameter values in a dedicated
392
- section of the docstring.
393
404
394
- * Generally, do not add markup to types for ``Parameters `` and ``Returns ``.
395
- This is usually not needed because Sphinx will link them automatically and
396
- would unnecessarily clutter the docstring. However, it does seem to fail in
397
- some situations. If you encounter such a case, you are allowed to add markup:
405
+ Quotes for strings
406
+ Matplotlib does not have a convention whether to use single-quotes or
407
+ double-quotes. There is a mixture of both in the current code.
398
408
399
- .. code-block :: rst
409
+ Use simple single or double quotes when giving string values, e.g. :: rst
400
410
401
- Returns
402
- -------
403
- lines : `~matplotlib.collections.LineCollection`
411
+ .. code-block :: rst
404
412
405
- * rcParams can be referenced with the custom ``:rc: `` role:
406
- :literal: `:rc:\` foo\` ` yields ``rcParams["foo"] ``.
413
+ If 'tight', try to figure out the tight bbox of the figure.
407
414
408
- Deprecated formatting conventions
409
- ---------------------------------
410
- * Formerly, we have used square brackets for explicit parameter lists
411
- `` ['solid' | 'dashed' | 'dotted'] ``. With numpydoc we have switched to their
412
- standard using curly braces `` {'solid', 'dashed', 'dotted'} `` .
415
+ Parameter type descriptions
416
+ The main goal for parameter type descriptions is to be readable and
417
+ understandable by humans. If the possible types are too complex use a
418
+ simplification for the type description and explain the type more
419
+ precisely in the text .
413
420
414
- Linking to other code
415
- ---------------------
416
- To link to other methods, classes, or modules in Matplotlib you can encase
417
- the name to refer to in back ticks, for example:
421
+ Generally, the `numpydoc docstring guide `_ conventions apply. The following
422
+ rules expand on them where the numpydoc conventions are not specific.
418
423
419
- .. code-block :: python
424
+ Use `` float `` for a type that can be any number.
420
425
421
- `~ matplotlib.collections.LineCollection`
426
+ Use ``array-like `` for homogeneous numeric sequences, which could
427
+ typically be a numpy.array. Dimensionality may be specified using ``2D ``,
428
+ ``3D ``, ``n-dimensional ``. If you need to have variables denoting the
429
+ sizes of the dimensions, use capital letters in brackets
430
+ (``array-like (M, N) ``). When refering to them in the text they are easier
431
+ read and no special formatting is needed.
422
432
423
- It is also possible to add links to code in Python, Numpy, Scipy, or Pandas.
424
- Sometimes it is tricky to get external Sphinx linking to work; to check that
425
- a something exists to link to the following shell command outputs a list of all
426
- objects that can be referenced (in this case for Numpy)::
433
+ ``float `` is the implicit default dtype for array-likes. For other dtypes
434
+ use ``array-like of int ``.
427
435
428
- python -m sphinx.ext.intersphinx 'https://docs.scipy.org/doc/numpy/objects.inv'
436
+ Some possible uses::
429
437
438
+ 2D array-like
439
+ array-like (N)
440
+ array-like (M, N)
441
+ array-like (M, N, 3)
442
+ array-like of int
430
443
431
- Function arguments
432
- ------------------
433
- Function arguments and keywords within docstrings should be referred to using
434
- the ``*emphasis* `` role. This will keep Matplotlib's documentation consistent
435
- with Python's documentation:
444
+ Non-numeric homogeneous sequences are described as lists, e.g.::
436
445
437
- .. code-block :: rst
446
+ list of str
447
+ list of `.Artist`
438
448
439
- Here is a description of *argument*
449
+ Referencing types
450
+ Generally, the rules from referring-to-other-code _ apply. More specifically:
440
451
441
- Do not use the ```default role` ``:
452
+ Use full references ```~matplotlib.colors.Normalize` `` with an
453
+ abbreviation tilde in parameter types. While the full name helps the
454
+ reader of plain text docstrings, the HTML does not need to show the full
455
+ name as it links to it. Hence, the ``~ ``-shortening keeps it more readable.
442
456
457
+ Use abbreviated links ```.Normalize` `` in the text.
443
458
444
- .. code-block :: rst
459
+ .. code-block :: rst
445
460
446
- Do not describe `argument` like this.
461
+ norm : `~matplotlib.colors.Normalize`, optional
462
+ A `.Normalize` instance is used to scale luminance data to 0, 1.
447
463
448
- nor the ````literal`` `` role:
464
+ ``See also `` sections
465
+ Sphinx automatically links code elements in the definition blocks of ``See
466
+ also `` sections. No need to use backticks there::
449
467
450
- .. code-block :: rst
468
+ See also
469
+ --------
470
+ vlines : vertical lines
471
+ axhline: horizontal line across the axes
451
472
452
- Do not describe ``argument`` like this.
473
+ Wrapping parameter lists
474
+ Long parameter lists should be wrapped using a ``\ `` for continuation and
475
+ starting on the new line without any indent:
476
+
477
+ .. code-block :: python
478
+
479
+ def add_axes (self , * args , ** kwargs ):
480
+ """
481
+ ...
482
+
483
+ Parameters
484
+ ----------
485
+ projection :
486
+ {'aitoff', 'hammer', 'lambert', 'mollweide', 'polar', \
487
+ 'rectilinear'}, optional
488
+ The projection type of the axes.
489
+
490
+ ...
491
+ """
492
+
493
+ Alternatively, you can describe the valid parameter values in a dedicated
494
+ section of the docstring.
495
+
496
+ rcParams
497
+ rcParams can be referenced with the custom ``:rc: `` role:
498
+ :literal: `:rc:\` foo\` ` yields ``rcParams["foo"] ``. Use `= [default-val] `
499
+ to indicate the default value of the parameter. The default value should be
500
+ literal, i.e. enclosed in double backticks. For simplicity these may be
501
+ omitted for string default values.
502
+
503
+ .. code-block :: rst
504
+
505
+ If not provided, defaults to :rc:`figure.figsize` = ``[6.4, 4.8]``.
506
+ If not provided, defaults to :rc:`figure.facecolor` = 'w'.
507
+
508
+ Deprecated formatting conventions
509
+ ---------------------------------
510
+ Formerly, we have used square brackets for explicit parameter lists
511
+ ``['solid' | 'dashed' | 'dotted'] ``. With numpydoc we have switched to their
512
+ standard using curly braces ``{'solid', 'dashed', 'dotted'} ``.
453
513
454
514
Setters and getters
455
515
-------------------
@@ -460,6 +520,12 @@ By convention, these setters and getters are named ``set_PROPERTYNAME`` and
460
520
``get_PROPERTYNAME ``; the list of properties thusly defined on an artist and
461
521
their values can be listed by the `~.pyplot.setp ` and `~.pyplot.getp ` functions.
462
522
523
+ .. note ::
524
+
525
+ ``ACCEPTS `` blocks have recently become optional. You may now use a
526
+ numpydoc ``Parameters `` block because the accepted values can now be read
527
+ from the type description of the first parameter.
528
+
463
529
Property setter methods should indicate the values they accept using a (legacy)
464
530
special block in the docstring, starting with ``ACCEPTS ``, as follows:
465
531
@@ -493,7 +559,6 @@ Sphinx by making it a ReST comment (i.e. use ``.. ACCEPTS:``):
493
559
"""
494
560
495
561
496
-
497
562
Keyword arguments
498
563
-----------------
499
564
@@ -812,4 +877,4 @@ Some helpful functions::
812
877
.. _index : http://www.sphinx-doc.org/markup/para.html#index-generating-markup
813
878
.. _`Sphinx Gallery` : https://sphinx-gallery.readthedocs.io/en/latest/
814
879
.. _references : http://www.sphinx-doc.org/en/stable/markup/inline.html
815
- .. _`numpy documentation howto ` : https://github.com/numpy/numpy/blob/master/doc/HOWTO_DOCUMENT.rst.txt
880
+ .. _`numpydoc docstring guide ` : https://numpydoc.readthedocs.io/en/latest/format.html
0 commit comments