matplotlib.pyplot
#
matplotlib.pyplot
is a state-based interface to matplotlib. It providesan implicit, MATLAB-like, way of plotting. It also opens figures on yourscreen, and acts as the figure GUI manager.
pyplot is mainly intended for interactive plots and simple cases ofprogrammatic plot generation:
importnumpyasnpimportmatplotlib.pyplotaspltx=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, thoughpyplot is still usually used to create the figure and often the Axes in thefigure. Seepyplot.figure
,pyplot.subplots
, andpyplot.subplot_mosaic
to create figures, andAxes API for the plotting methods on an Axes:
importnumpyasnpimportmatplotlib.pyplotaspltx=np.arange(0,5,0.1)y=np.sin(x)fig,ax=plt.subplots()ax.plot(x,y)plt.show()
SeeMatplotlib Application Interfaces (APIs) for an explanation of the tradeoffs between theimplicit and explicit interfaces.
Managing Figure and Axes#
Add an Axes to the current figure and make it the current Axes. | |
Clear the current Axes. | |
Clear the current figure. | |
Close a figure window, and unregister it from pyplot. | |
Remove an | |
Return whether the figure with the given id exists. | |
Create a new figure, or activate an existing figure. | |
Get the current Axes. | |
Get the current figure. | |
Return a list of existing figure labels. | |
Return a list of existing figure numbers. | |
Set the current Axes toax and the current Figure to the parent ofax. | |
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. | |
Create a figure and a set of subplots. | |
Make and return a second Axes that shares thex-axis. | |
Make and return a second Axes that shares they-axis. |
Adding data to the plot#
Basic#
Plot y versus x as lines and/or markers. | |
Plot y versus x as lines and/or markers with attached errorbars. | |
A scatter plot ofy vs. | |
Make a step plot. | |
Make a plot with log scaling on both the x- and y-axis. | |
Make a plot with log scaling on the x-axis. | |
Make a plot with log scaling on the y-axis. | |
Fill the area between two horizontal curves. | |
Fill the area between two vertical curves. | |
Make a bar plot. | |
Make a horizontal bar plot. | |
Label a bar plot. | |
Create a stem plot. | |
Plot identical parallel lines at the given positions. | |
Plot a pie chart. | |
Draw a stacked area plot or a streamgraph. | |
Plot a horizontal sequence of rectangles. | |
Plot vertical lines at eachx fromymin toymax. | |
Plot horizontal lines at eachy fromxmin toxmax. | |
Plot filled polygons. | |
Make a polar plot. |
Spans#
Spectral#
Plot the autocorrelation ofx. | |
Plot the angle spectrum. | |
Plot the coherence betweenx andy. | |
Plot the cross-spectral density. | |
Plot the magnitude spectrum. | |
Plot the phase spectrum. | |
Plot the power spectral density. | |
Plot a spectrogram. | |
Plot the cross correlation betweenx andy. |
Statistics#
Compute and plot the empirical cumulative distribution function ofx. | |
Draw a box and whisker plot. | |
Make a violin plot. |
Binned#
Contours#
2D arrays#
Display data as an image, i.e., on a 2D regular raster. | |
Display a 2D array as a matrix in a new figure window. | |
Create a pseudocolor plot with a non-regular rectangular grid. | |
Create a pseudocolor plot with a non-regular rectangular grid. | |
Plot the sparsity pattern of a 2D array. | |
Add a non-resampled image to the figure. |
Unstructured triangles#
Draw an unstructured triangular grid as lines and/or markers. | |
Create a pseudocolor plot of an unstructured triangular grid. | |
Draw contour lines on an unstructured triangular grid. | |
Draw contour regions on an unstructured triangular grid. |
Text and annotations#
Vector fields#
Plot a 2D field of wind barbs. | |
Plot a 2D field of arrows. | |
Add a key to a quiver plot. | |
Draw streamlines of a vector flow. |
Axis configuration#
Autoscale the axis view to the data (toggle). | |
Convenience method to get or set some axis properties. | |
Turn the Axes box on or off on the current Axes. | |
Configure the grid lines. | |
Control behavior of major tick locators. | |
Remove minor ticks from the Axes. | |
Display minor ticks on the Axes. | |
Get or set the radial gridlines on the current polar plot. | |
Get or set the theta gridlines on the current polar plot. | |
Change the appearance of ticks, tick labels, and gridlines. | |
Configure the | |
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. | |
Add a centered super title to the figure. | |
Set a title for the Axes. |
Layout#
Set or retrieve margins around the data for autoscaling axis limits. | |
Adjust the subplot layout parameters. | |
Launch a subplot tool window for a figure. | |
Adjust the padding between and around subplots. |
Colormapping#
Set the color limits of the current image. | |
Add a colorbar to a plot. | |
Get the current colorable artist. | |
Set the current image. | |
Get a colormap instance, defaulting to rc values ifname is None. | |
Set the default colormap, and applies it to the current image if any. | |
Read an image from a file into an array. | |
Colormap and save an array as an image file. |
Colormaps are available via the colormap registrymatplotlib.colormaps
. Forconvenience this registry is available inpyplot
as
- matplotlib.pyplot.colormaps[source]#
Container for colormaps that are known to Matplotlib by name.
The universal registry instance is
matplotlib.colormaps
. There should beno need for users to instantiateColormapRegistry
themselves.Read access uses a dict-like interface mapping names to
Colormap
s:importmatplotlibasmplcmap=mpl.colormaps['viridis']
Returned
Colormap
s are copies, so that their modification does notchange 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:
frommatplotlibimportcolormapslist(colormaps)
Additionally, there are shortcut functions to set builtin colormaps; e.g.plt.viridis()
is equivalent toplt.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
. Thereshould be no need for users to instantiateColorSequenceRegistry
themselves.Read access uses a dict-like interface mapping names to lists of colors:
importmatplotlibasmplcolors=mpl.color_sequences['tab10']
For a list of built in color sequences, seeNamed color sequences.The returned lists are copies, so that their modification does not changethe global definition of the color sequence.
Additional color sequences can be added via
ColorSequenceRegistry.register
:mpl.color_sequences.register('rgb',['r','g','b'])
Configuration#
Set the current | |
Return a context manager for temporarily changing rcParams. | |
Restore the |
Output#
Redraw the current figure. | |
Redraw the current figure if in interactive mode. | |
Disable interactive mode. | |
Enable interactive mode. | |
Connect to the display hook of the current shell. | |
Return whether plots are updated after every plotting command. | |
Run the GUI event loop forinterval seconds. | |
Save the current figure as an image or vector graphic to a file. | |
Display all open figures. | |
Set the pyplot backend. | |
Disconnect from the display hook of the current shell. |
Other#
Bind functionfunc to events. | |
Disconnect the callback with idcid. | |
Find artist objects. | |
Return the value of an | |
Return the value of an | |
Return the figure manager of the current figure. | |
Blocking call to interact with a figure. | |
Create a new figure manager instance. | |
Configure Matplotlib's logging levels. | |
Set one or more properties on an | |
Blocking call to interact with the figure. | |
Turn onxkcd sketch-style drawing mode. |