Writing documentation — Matplotlib 2.2.0 documentation
You are reading an old version of the documentation (v2.2.0). For the latest version see https://matplotlib.org/stable/devel/documenting_mpl.html Version 2.2.0 Fork me on GitHub Navigation index modules | next | previous | home | examples | tutorials | pyplot | docs » The Matplotlib Developers’ Guide » Table Of Contents Writing documentation Getting started General file structure Installing dependencies Building the docs Writing ReST pages Formatting and style conventions Section name formatting Function arguments Referring to other documents and sections Referring to other code Including figures and files Writing docstrings Example docstring Formatting conventions Deprecated formatting conventions Linking to other code Function arguments Setters and getters Keyword arguments Adding figures Writing examples and tutorials Miscellaneous Adding animations Generating inheritance diagrams Emacs helpers Related Topics Documentation overview The Matplotlib Developers’ Guide Previous: Developer’s tips for testing Next: Plot directive documentation This Page Show Source Quick search Writing documentation ¶ Getting started ¶ General file structure ¶ All documentation is built from the doc/ directory.
This directory contains both reStructuredText ( ReST ; .rst ) files that contain pages in the documentation and configuration files for Sphinx . The .rst files are kept in doc/users , doc/devel , doc/api and doc/faq . The main entry point is doc/index.rst , which pulls in the index.rst file for the users guide, developers guide, api reference, and FAQs. The documentation suite is built as a single document in order to make the most effective use of cross referencing.
Sphinx also creates .rst files that are staged in doc/api from the docstrings of the classes in the Matplotlib library. Except for doc/api/api_changes/ , these .rst files are created when the documentation is built. Similarly, the contents of docs/gallery and docs/tutorials are generated by the Sphinx Gallery from the sources in examples and tutorials . These sources consist of python scripts that have ReST documentation built into their comments.
Don’t directly edit the .rst files in docs/gallery and docs/tutorials as they are regenerated when the documentation are built. Installing dependencies ¶ The documentation for Matplotlib is generated from reStructuredText ( ReST ) using the Sphinx documentation generation tool. There are several extra requirements that are needed to build the documentation. They are listed in doc-requirements.txt and listed below: Sphinx>=1.3, !=1.5.0, !=1.6.4 colorspacious IPython mock numpydoc>=0.4 Pillow sphinx-gallery>=0.1.12 graphviz Note You’ll need a minimal working LaTeX distribution for many examples to run.
Graphviz is not a Python package, and needs to be installed separately. Building the docs ¶ The documentation sources are found in the doc/ directory in the trunk. The configuration file for Sphinx is doc/conf.py . It controls which directories Sphinx parses, how the docs are built, and how the extensions are used. To build the documentation in html format, cd into doc/ and run: make html Other useful invocations include # Delete built files.
May help if you get errors about missing paths or # broken links. make clean # Build pdf docs. make latexpdf The SPHINXOPTS variable is set to -W by default to turn warnings into errors. To unset it, use make SPHINXOPTS = html You can use the O variable to set additional options: make O=-j4 html runs a parallel build with 4 processes. make O=-Dplot_formats=png:100 html saves figures in low resolution. make O=-Dplot_gallery=0 html skips the gallery build.
Multiple options can be combined using e.g. make O='-j4 -Dplot_gallery=0' html . On Windows, options needs to be set as environment variables, e.g. set O=-W -j4 & make html . Writing ReST pages ¶ Most documentation is either in the docstring of individual classes and methods, in explicit .rst files, or in examples and tutorials. All of these use the ReST syntax. Users should look at the ReST documentation for a full description.
But some specific hints and conventions Matplotlib uses are useful for creating documentation. Formatting and style conventions ¶ It is useful to strive for consistency in the Matplotlib documentation. Here are some formatting and style conventions that are used. Section name formatting ¶ For everything but top-level chapters, use Upper lower for section titles, e.g., Possible hangups rather than Possible Hangups Function arguments ¶ Function arguments and keywords within docstrings should be referred to using the *emphasis* role.
This will keep Matplotlib’s documentation consistent with Python’s documentation: Here is a description of *argument* Do not use the `default role` : Do not describe `argument` like this. As per the next section, this syntax will (unsuccessfully) attempt to resolve the argument as a link to a class or method in the library. nor the ``literal`` role: Do not describe ``argument`` like this. Referring to other documents and sections ¶ Sphinx allows internal references between documents.
Documents can be linked with the :doc: directive: See the :doc: `/faq/installing_faq` See the tutorial :doc: `/tutorials/introductory/sample_plots` See the example :doc: `/gallery/lines_bars_and_markers/simple_plot` will render as: See the Installation See the tutorial Sample plots in Matplotlib See the example Simple Plot Sections can also be given reference names. For instance from the Installation link: .. _clean-install: How to completely remove Matplotlib =================================== Occasionally, problems with Matplotlib can be solved with a clean... and refer to it using the standard reference syntax: See :ref: `clean-install` will give the following link: How to completely remove Matplotlib To maximize internal consistency in section labeling and references, use hyphen separated, descriptive labels for section references.
Keep in mind that contents may be reorganized later, so avoid top level names in references like user or devel or faq unless necessary, because for example the FAQ “what is a backend?” could later become part of the users guide, so the label: .. _what-is-a-backend: is better than: .. _faq-backend: In addition, since underscores are widely used by Sphinx itself, use hyphens to separate words. Referring to other code ¶ To link to other methods, classes, or modules in Matplotlib you can use back ticks, for example: `~matplotlib.collections.LineCollection` returns a link to the documentation of LineCollection .
For the full path of the class to be shown, omit the tilde: `matplotlib.collections.LineCollection` to get matplotlib.collections.LineCollection . It is often not necessary to fully specify the class hierarchy unless there is a namespace collision between two packages: `~.LineCollection` links just as well: LineCollection . Other packages can also be linked via intersphinx : `numpy.mean` will return this link: numpy.mean .
This works for Python, Numpy, Scipy, and Pandas (full list is in doc/conf.py ). Sometimes it is tricky to get external Sphinx linking to work; to check that a something exists to link to the following shell command outputs a list of all objects that can be referenced (in this case for Numpy): python - m sphinx . ext . intersphinx 'https://docs.scipy.org/doc/numpy/objects.inv' Including figures and files ¶ Image files can directly included in pages with the image:: directive. e.g., users/navigation_toolbar.rst displays the toolbar icons with a call to a static image: .. image :: ../ _static / toolbar . png as rendered on the page: Interactive navigation .
Files can be included verbatim. For instance the matplotlibrc file is important for customizing Matplotlib, and is included verbatim in the tutorial in Customizing matplotlib : .. literalinclude :: ../../ _static / matplotlibrc This is rendered at the bottom of Customizing matplotlib . Note that this is in
The rest of this page is on the live site.