matplotlib.pyplot — Matplotlib 3.10.3 documentation (original) (raw)

matplotlib.pyplot is a state-based interface to matplotlib. It provides an implicit, MATLAB-like, way of plotting. It also opens figures on your screen, and acts as the figure GUI manager.

pyplot is mainly intended for interactive plots and simple cases of programmatic plot generation:

import numpy as np import matplotlib.pyplot as plt

x = np.arange(0, 5, 0.1) y = np.sin(x) plt.plot(x, y) plt.show()

The explicit object-oriented API is recommended for complex plots, though pyplot is still usually used to create the figure and often the Axes in the figure. See pyplot.figure, pyplot.subplots, andpyplot.subplot_mosaic to create figures, andAxes API for the plotting methods on an Axes:

import numpy as np import matplotlib.pyplot as plt

x = np.arange(0, 5, 0.1) y = np.sin(x) fig, ax = plt.subplots() ax.plot(x, y) plt.show()

See Matplotlib Application Interfaces (APIs) for an explanation of the tradeoffs between the implicit and explicit interfaces.

Managing Figure and Axes#

Adding data to the plot#

Basic#

Spans#

Spectral#

Statistics#

Binned#

Contours#

2D arrays#

Unstructured triangles#

Text and annotations#

Vector fields#

Axis configuration#

Layout#

Colormapping#

Colormaps are available via the colormap registry matplotlib.colormaps. For convenience this registry is available in pyplot as

matplotlib.pyplot.colormaps[source]#

Container for colormaps that are known to Matplotlib by name.

The universal registry instance is matplotlib.colormaps. There should be no need for users to instantiate ColormapRegistry themselves.

Read access uses a dict-like interface mapping names to Colormaps:

import matplotlib as mpl cmap = mpl.colormaps['viridis']

Returned Colormaps are copies, so that their modification does not change the global definition of the colormap.

Additional colormaps can be added via ColormapRegistry.register:

mpl.colormaps.register(my_colormap)

To get a list of all registered colormaps, you can do:

from matplotlib import colormaps list(colormaps)

Additionally, there are shortcut functions to set builtin colormaps; e.g.plt.viridis() is equivalent to plt.set_cmap('viridis').

matplotlib.pyplot.color_sequences[source]#

Container for sequences of colors that are known to Matplotlib by name.

The universal registry instance is matplotlib.color_sequences. There should be no need for users to instantiate ColorSequenceRegistrythemselves.

Read access uses a dict-like interface mapping names to lists of colors:

import matplotlib as mpl colors = mpl.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.

Additional color sequences can be added viaColorSequenceRegistry.register:

mpl.color_sequences.register('rgb', ['r', 'g', 'b'])

Configuration#

Output#

Other#