44Writing documentation
55=====================
66
7+ .. contents :: Contents
8+ :depth: 2
9+ :local:
10+ :backlinks: top
11+ :class: multicol-toc
12+
13+
714Getting started
815===============
916
@@ -213,38 +220,52 @@ is better than:
213220 In addition, since underscores are widely used by Sphinx itself, use
214221hyphens to separate words.
215222
223+ .. _referring-to-other-code :
216224
217225Referring to other code
218226-----------------------
219227
220228To link to other methods, classes, or modules in Matplotlib you can use
221229back ticks, for example:
222230
223- .. code-block :: python
231+ .. code-block :: rst
224232
225- `~ matplotlib.collections.LineCollection`
233+ `matplotlib.collections.LineCollection`
226234
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 `.
230236
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.
232239
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:
234242
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:
243+ .. code-block :: rst
238244
239- .. code-block :: python
245+ `.LineCollection`
240246
241- ` ~ .LineCollection`
247+ and the link still works: ` .LineCollection `.
242248
243- links just as well: `~.LineCollection `.
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:
244251
245- Other packages can also be linked via ``intersphinx ``:
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
260+
261+ `~.pyplot.plot` or `~.Axes.plot`
246262
247- .. code-block :: Python
263+ will render as `~.pyplot.plot ` or `~.Axes.plot `.
264+
265+ Other packages can also be linked via
266+ `intersphinx <http://www.sphinx-doc.org/en/master/ext/intersphinx.html >`_:
267+
268+ .. code-block :: rst
248269
249270 `numpy.mean`
250271
@@ -296,13 +317,19 @@ when the documentation is built.
296317Writing docstrings
297318==================
298319
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
306333:file: `doc/api ` directory and form the reference documentation for the
307334library.
308335
@@ -313,21 +340,21 @@ An example docstring looks like:
313340
314341.. code-block :: python
315342
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 ):
318345 """
319346 Plot horizontal lines at each *y* from *xmin* to *xmax*.
320347
321348 Parameters
322349 ----------
323- y : scalar or sequence of scalar
350+ y : float or array-like
324351 y-indexes where to plot the lines.
325352
326- xmin, xmax : scalar or 1D array_like
353+ xmin, xmax : float or array-like
327354 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.
329356
330- colors : array_like of colors, optional, default: 'k'
357+ colors : array-like of colors, optional, default: 'k'
331358
332359 linestyles : {'solid', 'dashed', 'dashdot', 'dotted'}, optional
333360
@@ -352,104 +379,143 @@ See the `~.Axes.hlines` documentation for how this renders.
352379The Sphinx _ website also contains plenty of documentation _ concerning ReST
353380markup and working with Sphinx in general.
354381
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-
362382Formatting conventions
363383----------------------
364384
365- The basic docstring conventions are covered in the `numpy documentation howto `_
385+ The basic docstring conventions are covered in the `numpydoc docstring guide `_
366386and the Sphinx _ documentation. Some Matplotlib-specific formatting conventions
367387to keep in mind:
368388
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:
371393
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
374395
375- .. code-block :: python
396+ If *linestyles* is *None*, the 'solid' is used.
376397
377- def add_axes (self , * args , ** kwargs ):
378- """
379- ...
398+ Do not use the ```default role` `` or the ````literal`` `` role:
380399
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
387401
388- ...
389- """
402+ Neither `argument` nor ``argument`` should be used.
390403
391- Alternatively, you can describe the valid parameter values in a dedicated
392- section of the docstring.
393404
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.
398408
399- .. code-block :: rst
409+ Use simple single or double quotes when giving string values, e.g. :: rst
400410
401- Returns
402- -------
403- lines : `~matplotlib.collections.LineCollection`
411+ .. code-block :: rst
404412
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.
407414
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 .
413420
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.
418423
419- .. code-block :: python
424+ Use `` float `` for a type that can be any number.
420425
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.
422432
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 ``.
427435
428- python -m sphinx.ext.intersphinx 'https://docs.scipy.org/doc/numpy/objects.inv'
436+ Some possible uses::
429437
438+ 2D array-like
439+ array-like (N)
440+ array-like (M, N)
441+ array-like (M, N, 3)
442+ array-like of int
430443
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.::
436445
437- .. code-block :: rst
446+ list of str
447+ list of `.Artist`
438448
439- Here is a description of *argument*
449+ Referencing types
450+ Generally, the rules from referring-to-other-code _ apply. More specifically:
440451
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.
442456
457+ Use abbreviated links ```.Normalize` `` in the text.
443458
444- .. code-block :: rst
459+ .. code-block :: rst
445460
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.
447463
448- nor the `` ``literal`` `` role :
464+ .. note : :
449465
450- .. code-block :: rst
466+ Sphinx claims to automatically link types in ``Parameters `` and
467+ ``Returns `` sections. However, this does currently not work reliably.
468+ Therefore we still have to enclose them in backticks.
451469
452- Do not describe ``argument`` like this.
470+ ``See also `` sections
471+ Sphinx automatically links code elements in the definition blocks of ``See
472+ also `` sections. No need to use backticks there::
473+
474+ See also
475+ --------
476+ vlines : vertical lines
477+ axhline: horizontal line across the axes
478+
479+ Wrapping parameter lists
480+ Long parameter lists should be wrapped using a ``\ `` for continuation and
481+ starting on the new line without any indent:
482+
483+ .. code-block :: python
484+
485+ def add_axes (self , * args , ** kwargs ):
486+ """
487+ ...
488+
489+ Parameters
490+ ----------
491+ projection :
492+ {'aitoff', 'hammer', 'lambert', 'mollweide', 'polar', \
493+ 'rectilinear'}, optional
494+ The projection type of the axes.
495+
496+ ...
497+ """
498+
499+ Alternatively, you can describe the valid parameter values in a dedicated
500+ section of the docstring.
501+
502+ rcParams
503+ rcParams can be referenced with the custom ``:rc: `` role:
504+ :literal: `:rc:\` foo\` ` yields ``rcParams["foo"] ``. Use `= [default-val] `
505+ to indicate the default value of the parameter. The default value should be
506+ literal, i.e. enclosed in double backticks. For simplicity these may be
507+ omitted for string default values.
508+
509+ .. code-block :: rst
510+
511+ If not provided, defaults to :rc:`figure.figsize` = ``[6.4, 4.8]``.
512+ If not provided, defaults to :rc:`figure.facecolor` = 'w'.
513+
514+ Deprecated formatting conventions
515+ ---------------------------------
516+ Formerly, we have used square brackets for explicit parameter lists
517+ ``['solid' | 'dashed' | 'dotted'] ``. With numpydoc we have switched to their
518+ standard using curly braces ``{'solid', 'dashed', 'dotted'} ``.
453519
454520Setters and getters
455521-------------------
@@ -460,6 +526,12 @@ By convention, these setters and getters are named ``set_PROPERTYNAME`` and
460526``get_PROPERTYNAME ``; the list of properties thusly defined on an artist and
461527their values can be listed by the `~.pyplot.setp ` and `~.pyplot.getp ` functions.
462528
529+ .. note ::
530+
531+ ``ACCEPTS `` blocks have recently become optional. You may now use a
532+ numpydoc ``Parameters `` block because the accepted values can now be read
533+ from the type description of the first parameter.
534+
463535Property setter methods should indicate the values they accept using a (legacy)
464536special block in the docstring, starting with ``ACCEPTS ``, as follows:
465537
@@ -493,7 +565,6 @@ Sphinx by making it a ReST comment (i.e. use ``.. ACCEPTS:``):
493565 """
494566
495567
496-
497568 Keyword arguments
498569-----------------
499570
@@ -812,4 +883,4 @@ Some helpful functions::
812883.. _index : http://www.sphinx-doc.org/markup/para.html#index-generating-markup
813884.. _`Sphinx Gallery` : https://sphinx-gallery.readthedocs.io/en/latest/
814885.. _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
886+ .. _`numpydoc docstring guide ` : https://numpydoc.readthedocs.io/en/latest/format.html
0 commit comments