matplotlib.pyplot
#
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)
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
, and
pyplot.subplot_mosaic
to create figures, and
Axes 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)
See Matplotlib Application Interfaces (APIs) for an explanation of the tradeoffs between the implicit and explicit interfaces.
Plotting commands#
Plot the autocorrelation of x. |
|
Plot the angle spectrum. |
|
Annotate the point xy with text text. |
|
Add an arrow to the Axes. |
|
Autoscale the axis view to the data (toggle). |
|
Add an Axes to the current figure and make it the current Axes. |
|
Add a horizontal line across the Axes. |
|
Add a horizontal span (rectangle) across the Axes. |
|
Convenience method to get or set some axis properties. |
|
Add an infinitely long straight line. |
|
Add a vertical line across the Axes. |
|
Add a vertical span (rectangle) across the Axes. |
|
Make a bar plot. |
|
Label a bar plot. |
|
Plot a 2D field of barbs. |
|
Make a horizontal bar plot. |
|
Turn the axes box on or off on the current axes. |
|
Draw a box and whisker plot. |
|
Plot a horizontal sequence of rectangles. |
|
Clear the current axes. |
|
Label a contour plot. |
|
Clear the current figure. |
|
Set the color limits of the current image. |
|
Close a figure window. |
|
Plot the coherence between x and y. |
|
Add a colorbar to a plot. |
|
Plot contour lines. |
|
Plot filled contours. |
|
Plot the cross-spectral density. |
|
Remove an |
|
Redraw the current figure. |
|
Redraw the current figure if in interactive mode. |
|
Plot y versus x as lines and/or markers with attached errorbars. |
|
Plot identical parallel lines at the given positions. |
|
Add a non-resampled image to the figure. |
|
Place a legend on the figure. |
|
Return whether the figure with the given id exists. |
|
Add text to figure. |
|
Create a new figure, or activate an existing figure. |
|
Plot filled polygons. |
|
Fill the area between two horizontal curves. |
|
Fill the area between two vertical curves. |
|
Find artist objects. |
|
Get the current Axes. |
|
Get the current figure. |
|
Get the current colorable artist. |
|
Return the value of an |
|
Get a colormap instance, defaulting to rc values if name is None. |
|
Return a list of existing figure labels. |
|
Return a list of existing figure numbers. |
|
Return the value of an |
|
Configure the grid lines. |
|
Make a 2D hexagonal binning plot of points x, y. |
|
Compute and plot a histogram. |
|
Make a 2D histogram plot. |
|
Plot horizontal lines at each y from xmin to xmax. |
|
Read an image from a file into an array. |
|
Save an array as an image file. |
|
Display data as an image, i.e., on a 2D regular raster. |
|
Connect to the display hook of the current shell. |
|
Disable interactive mode. |
|
Enable interactive mode. |
|
Return whether plots are updated after every plotting command. |
|
Place a legend on the Axes. |
|
Control behavior of major tick locators. |
|
Make a plot with log scaling on both the x and y axis. |
|
Plot the magnitude spectrum. |
|
Set or retrieve autoscaling margins. |
|
Display an array as a matrix in a new figure window. |
|
Remove minor ticks from the Axes. |
|
Display minor ticks on the Axes. |
|
Run the GUI event loop for interval seconds. |
|
Create a pseudocolor plot with a non-regular rectangular grid. |
|
Create a pseudocolor plot with a non-regular rectangular grid. |
|
Plot the phase spectrum. |
|
Plot a pie chart. |
|
Plot y versus x as lines and/or markers. |
|
[Discouraged] Plot coercing the axis to treat floats as dates. |
|
Make a polar plot. |
|
Plot the power spectral density. |
|
Plot a 2D field of arrows. |
|
Add a key to a quiver plot. |
|
Set the current |
|
Return a context manager for temporarily changing rcParams. |
|
Restore the |
|
Get or set the radial gridlines on the current polar plot. |
|
Save the current figure. |
|
Set the current Axes to ax and the current Figure to the parent of ax. |
|
A scatter plot of y vs. |
|
Set the current image. |
|
Make a plot with log scaling on the x axis. |
|
Make a plot with log scaling on the y axis. |
|
Set the default colormap, and applies it to the current image if any. |
|
Set Matplotlib's root logger and root logger handler level, creating the handler if it does not exist yet. |
|
Set one or more properties on an |
|
Display all open figures. |
|
Plot a spectrogram. |
|
Plot the sparsity pattern of a 2D array. |
|
Draw a stacked area plot. |
|
A stepwise constant function as a line with bounding edges or a filled plot. |
|
Create a stem plot. |
|
Make a step plot. |
|
Draw streamlines of a vector flow. |
|
Add an Axes to the current figure or retrieve an existing Axes. |
|
Create a subplot at a specific location inside a regular grid. |
|
Build a layout of Axes based on ASCII art or nested lists. |
|
Launch a subplot tool window for a figure. |
|
Create a figure and a set of subplots. |
|
Adjust the subplot layout parameters. |
|
Add a centered suptitle to the figure. |
|
Close all open figures and set the Matplotlib backend. |
|
Add a table to an |
|
Add text to the Axes. |
|
Get or set the theta gridlines on the current polar plot. |
|
Change the appearance of ticks, tick labels, and gridlines. |
|
Configure the |
|
Adjust the padding between and around subplots. |
|
Set a title for the Axes. |
|
Draw contour lines on an unstructured triangular grid. |
|
Draw contour regions on an unstructured triangular grid. |
|
Create a pseudocolor plot of an unstructured triangular grid. |
|
Draw an unstructured triangular grid as lines and/or markers. |
|
Make and return a second axes that shares the x-axis. |
|
Make and return a second axes that shares the y-axis. |
|
Disconnect from the display hook of the current shell. |
|
Make a violin plot. |
|
Plot vertical lines at each x from ymin to ymax. |
|
Plot the cross correlation between x and y. |
|
Turn on xkcd sketch-style drawing mode. |
|
Set the label for the x-axis. |
|
Get or set the x limits of the current axes. |
|
Set the xaxis' scale. |
|
Get or set the current tick locations and labels of the x-axis. |
|
Set the label for the y-axis. |
|
Get or set the y-limits of the current axes. |
|
Set the yaxis' scale. |
|
Get or set the current tick locations and labels of the y-axis. |
Other commands#
Bind function func to event s. |
|
Disconnect the callback with id cid. |
|
Return the figure manager of the current figure. |
|
Blocking call to interact with a figure. |
|
Create a new figure manager instance. |
|
Blocking call to interact with the figure. |
Colormaps#
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.
Experimental
While we expect the API to be final, we formally mark it as experimental for 3.5 because we want to keep the option to still adapt the API for 3.6 should the need arise.
The universal registry instance is
matplotlib.colormaps
. There should be no need for users to instantiateColormapRegistry
themselves.Read access uses a dict-like interface mapping names to
Colormap
s:import matplotlib as mpl cmap = mpl.colormaps['viridis']
Returned
Colormap
s 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)
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 instantiateColorSequenceRegistry
themselves.Read access uses a dict-like interface mapping names to lists of colors:
import matplotlib as mpl cmap = mpl.color_sequences['tab10']
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 via
ColorSequenceRegistry.register
:mpl.color_sequences.register('rgb', ['r', 'g', 'b'])