matplotlib.sphinxext.plot_directive
# A directive for including a Matplotlib plot in a Sphinx document#
This is a Sphinx extension providing a reStructuredText directive .. plot::
for including a plot in a Sphinx document.
In HTML output, .. plot::
will include a .png file with a link to a high-res .png and .pdf. In LaTeX output, it will include a .pdf.
The plot content may be defined in one of three ways:
A path to a source file as the argument to the directive:
.. plot:: path/to/plot.py
When a path to a source file is given, the content of the directive may optionally contain a caption for the plot:
.. plot:: path/to/plot.py The plot caption.
Additionally, one may specify the name of a function to call (with no arguments) immediately after importing the module:
.. plot:: path/to/plot.py plot_function1
Included as inline content to the directive:
.. plot:: import matplotlib.pyplot as plt plt.plot([1, 2, 3], [4, 5, 6]) plt.title("A plotting example")
Using doctest syntax:
.. plot:: A plotting example: >>> import matplotlib.pyplot as plt >>> plt.plot([1, 2, 3], [4, 5, 6])
The .. plot::
directive supports the following options:
:filename-prefix:
str
The base name (without the extension) of the outputted image and script files. The default is to use the same name as the input script, or the name of the RST document if no script is provided. The filename-prefix for each plot directive must be unique.
:format:
{'python', 'doctest'}
The format of the input. If unset, the format is auto-detected.
:include-source:
bool
Whether to display the source code. The default can be changed using the plot_include_source
variable in conf.py
(which itself defaults to False).
:show-source-link:
bool
Whether to show a link to the source in HTML. The default can be changed using the plot_html_show_source_link
variable in conf.py
(which itself defaults to True).
:context:
bool or str
If provided, the code will be run in the context of all previous plot directives for which the :context:
option was specified. This only applies to inline code plot directives, not those run from files. If the :context: reset
option is specified, the context is reset for this and future plots, and previous figures are closed prior to running the code. :context: close-figs
keeps the context but closes previous figures before running the code.
:nofigs:
bool
If specified, the code block will be run, but no figures will be inserted. This is usually useful with the :context:
option.
:caption:
str
If specified, the option's argument will be used as a caption for the figure. This overwrites the caption given in the content, when the plot is generated from a file.
Additionally, this directive supports all the options of the image directive, except for :target:
(since plot will add its own target). These include :alt:
, :height:
, :width:
, :scale:
, :align:
and :class:
.
The plot directive has the following configuration options:
Default value for the include-source option (default: False).
Whether to show a link to the source in HTML (default: True).
Code that should be executed before each plot. If None (the default), it will default to a string containing:
import numpy as np from matplotlib import pyplot as plt
Base directory, to which plot::
file names are relative to. If None or empty (the default), file names are relative to the directory where the file containing the directive is.
File formats to generate (default: ['png', 'hires.png', 'pdf']). List of tuples or strings:
[(suffix, dpi), suffix, ...]
that determine the file format and the DPI. For entries whose DPI was omitted, sensible defaults are chosen. When passing from the command line through sphinx_build the list should be passed as suffix:dpi,suffix:dpi, ...
Whether to show links to the files in HTML (default: True).
A dictionary containing any non-standard rcParams that should be applied before each plot (default: {}).
By default, rcParams are applied when :context:
option is not used in a plot directive. If set, this configuration option overrides this behavior and applies rcParams before each plot.
By default, the working directory will be changed to the directory of the example, so the code can get at its data files, if any. Also its path will be added to sys.path
so it can import any helper modules sitting beside it. This configuration option can be used to specify a central directory (also added to sys.path
) where data files and helper modules for all code are located.
Provide a customized template for preparing restructured text.
Allow the srcset image option for responsive image resolutions. List of strings with the multiplicative factors followed by an "x". e.g. ["2.0x", "1.5x"]. "2.0x" will create a png with the default "png" resolution from plot_formats, multiplied by 2. If plot_srcset is specified, the plot directive uses the matplotlib.sphinxext.figmpl_directive (instead of the usual figure directive) in the intermediary rst file that is generated. The plot_srcset option is incompatible with singlehtml builds, and an error will be raised.
The plot directive runs the code it is given, either in the source file or the code under the directive. The figure created (if any) is saved in the sphinx build directory under a subdirectory named plot_directive
. It then creates an intermediate rst file that calls a .. figure:
directive (or .. figmpl::
directive if plot_srcset
is being used) and has links to the *.png
files in the plot_directive
directory. These translations can be customized by changing the plot_template. See the source of matplotlib.sphinxext.plot_directive for the templates defined in TEMPLATE and TEMPLATE_SRCSET.
The .. plot::
directive, as documented in the module's docstring.
May the final argument contain whitespace?
May the directive have content?
Mapping of option names to validator functions.
Number of optional arguments after the required arguments.
Number of required directive arguments.
Run the plot directive.
To make plots referenceable, we need to move the reference from the "htmlonly" (or "latexonly") node to the actual figure node itself.
Return whether derived is out-of-date relative to original or any of the RST files included in it using the RST include directive (includes). derived and original are full paths, and includes is optionally a list of full paths which may have been included in the original.
Run a pyplot script and save the images in output_dir.
Save the images under output_dir with file names derived from output_base
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4