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