A procedural interface is provided by the companion pyplot module,
which may be imported directly, e.g.:
importmatplotlib.pyplotasplt
or using ipython:
ipython
at your terminal, followed by:
In[1]:%
matplotlib
In
[
2
]:
import
matplotlib.pyplot
as
plt
at the ipython shell prompt.
For the most part, direct use of the explicit object-oriented library is
encouraged when programming; the implicit pyplot interface is primarily for
working interactively. The exceptions to this suggestion are the pyplot
functions pyplot.figure, pyplot.subplot, pyplot.subplots, and
pyplot.savefig, which can greatly simplify scripting. See
Matplotlib Application Interfaces (APIs) for an explanation of the tradeoffs between the implicit
and explicit interfaces.
Setting the Matplotlib backend. This should be called before any
figure is created, because it is not possible to switch between
different GUI backends after that.
The following environment variables can be used to customize the behavior:
This is the directory used to store user customizations to
Matplotlib, as well as some caches to improve performance. If
MPLCONFIGDIR is not defined, HOME/.config/matplotlib
and HOME/.cache/matplotlib are used on Linux, and
HOME/.matplotlib on other platforms, if they are
writable. Otherwise, the Python standard library's tempfile.gettempdir
is used to find a base directory in which the matplotlib
subdirectory is created.
Matplotlib was initially written by John D. Hunter (1968-2012) and is now
developed and maintained by a host of others.
Occasionally the internal documentation (python docstrings) will refer
to MATLAB®, a registered trademark of The MathWorks, Inc.
Backend management
matplotlib.use(backend, *, force=True)
Select the backend used for rendering and GUI integration.
If pyplot is already imported, switch_backend is used
to switch the backend.
Parameters:
backend : str
The backend to switch to. This can either be one of the standard
backend names, which are case-insensitive:
Switching to an interactive backend is not possible if an unrelated
event loop has already been started (e.g., switching to GTK3Agg if a
TkAgg window has already been opened). Switching to a non-interactive
backend is always possible.
force : bool, default: True
If True (the default), raise an ImportError if the backend cannot be
set up (either because it fails to import, or because an incompatible
GUI interactive framework is already running); if False, silently
ignore the failure.
matplotlib.get_backend(*, auto_select=True)
Return the name of the current backend.
Parameters:
auto_select : bool, default: True
Whether to trigger backend resolution if no backend has been
selected so far. If True, this ensures that a valid backend
is returned. If False, this returns None if no backend has been
selected so far.
matplotlib.interactive(b)
Set whether to redraw after every plotting command (e.g. pyplot.xlabel).
matplotlib.is_interactive()
Return whether to redraw after every plotting command.
This is a dictionary-like variable that stores the current configuration
settings. Many of the values control styling, but others control
various aspects of Matplotlib's behavior.
rcParams changed both through the context manager invocation and
in the body of the context will be reset on context exit.
Parameters:
rc : dict
The rcParams to temporarily set.
matplotlib.rc(group, **kwargs)
Set the current rcParams. group is the grouping for the rc, e.g.,
for lines.linewidth the group is lines, for
axes.facecolor, the group is axes, and so on. Group may
also be a list or tuple of group names, e.g., (xtick, ytick).
kwargs is a dictionary attribute name/value pairs, e.g.,:
matplotlib.rcdefaults()
Restore the rcParams from Matplotlib's internal default style.
Style-blacklisted rcParams (defined in
matplotlib.style.core.STYLE_BLACKLIST) are not updated.
matplotlib.rc_file_defaults()
Restore the rcParams from the original rc file loaded by Matplotlib.
Style-blacklisted rcParams (defined in
matplotlib.style.core.STYLE_BLACKLIST) are not updated.
Return the string path of the configuration directory.
The directory is chosen as follows:
If the MPLCONFIGDIR environment variable is supplied, choose that.
On Linux, follow the XDG specification and look first in
$XDG_CONFIG_HOME, if defined, or $HOME/.config. On Windows,
use %LOCALAPPDATA%\matplotlib. On other platforms, choose
$HOME/.matplotlib.
If the chosen directory exists and is writable, use that as the
configuration directory.
Else, create a temporary directory, and use it as the configuration
directory.
matplotlib.matplotlib_fname()
Get the location of the config file.
The file location is determined in the following order
$PWD/matplotlibrc
$MATPLOTLIBRC if it is not a directory
$MATPLOTLIBRC/matplotlibrc
$MPLCONFIGDIR/matplotlibrc
On Linux,
$XDG_CONFIG_HOME/matplotlib/matplotlibrc (if
is defined)
matplotlib.get_data_path()
Return the path to Matplotlib data.
Logging
matplotlib.set_loglevel(level)
Configure Matplotlib's logging levels.
Matplotlib uses the standard library logging framework under the root
logger 'matplotlib'. This is a helper function to:
set Matplotlib's root logger level
set the root logger handler's level, creating the handler
if it does not exist yet
Typically, one should call set_loglevel("INFO") or
set_loglevel("DEBUG") to get additional debugging information.
Users or applications that are installing their own logging handlers
may want to directly manipulate logging.getLogger('matplotlib') rather
than use this function.
For backwards compatibility, the levels are case-insensitive, but
the capitalized version is preferred in analogy to logging.Logger.setLevel.
Notes
The first time this function is called, an additional handler is attached
to Matplotlib's root handler; this handler is reused every time and this
function simply manipulates the logger and handler's level.
Colormaps and color sequences
matplotlib.colormaps
Container for colormaps that are known to Matplotlib by name.
Read access uses a dict-like interface mapping names to lists of colors:
importmatplotlibasmplcolors=mpl.
Miscellaneous
classmatplotlib.MatplotlibDeprecationWarning
A class for issuing deprecation warnings for Matplotlib users.
matplotlib.get_cachedir()
Return the string path of the cache directory.
The procedure used to find the directory is the same as for
get_configdir, except using $XDG_CACHE_HOME/$HOME/.cache instead
on Linux. On Windows, uses %LOCALAPPDATA%\matplotlib (same as config).
Provisional
The auto_select flag is provisional. It may be changed or removed
without prior warning.
The following aliases are available to save typing for interactive users:
Alias
Property
'lw'
'linewidth'
'ls'
'linestyle'
'c'
'color'
'fc'
'facecolor'
'ec'
'edgecolor'
'mew'
'markeredgewidth'
'aa'
'antialiased'
'sans'
'sans-serif'
Thus you could abbreviate the above call as:
rc('lines',lw=2,c='r')
Note you can use python's kwargs dictionary facility to store
dictionaries of default parameters. e.g., you can customize the
font rc as follows:
font={'family':'monospace','weight':'bold','size':'large'}rc('font',**font)# pass in the font dict as kwargs
This enables you to easily switch between several configurations. Use
matplotlib.style.use('default') or rcdefaults() to
restore the default rcParams after changes.
Notes
Similar functionality is available by using the normal dict interface, i.e.
rcParams.update({"lines.linewidth":2,...}) (but rcParams.update
does not support abbreviations or grouping).
Use a specific style file. Call style.use('default') to restore the default style.
:
A file with Matplotlib rc settings.
use_default_template : bool
If True, initialize with default parameters before updating with those
in the given file. If False, the current configuration persists
and only the parameters specified in the file are updated.
If True, raise an error when the parser fails to convert a parameter.
use_default_template : bool
If True, initialize with default parameters before updating with those
in the given file. If False, the configuration class only contains the
parameters specified in the file. (Useful for updating dicts.)
$XDG_CONFIG_HOME
or $HOME/.config/matplotlib/matplotlibrc (if $XDG_CONFIG_HOME
is not defined)
On other platforms,
- $HOME/.matplotlib/matplotlibrc if $HOME is defined
Lastly, it looks in $MATPLOTLIBDATA/matplotlibrc, which should always
exist.
color_sequences
[
'tab10'
]
For a list of built in color sequences, see Named color sequences.
The returned lists are copies, so that their modification does not change
the global definition of the color sequence.