Below we describe several common approaches to plotting with Matplotlib.
Contents
The Pyplot API
The matplotlib.pyplot
module contains functions that allow you to generate many kinds of plots quickly. For examples that showcase the use of the matplotlib.pyplot
module, see the Pyplot tutorial or the Pyplot Examples. We also recommend that you look into the object-oriented approach to plotting, described below.
matplotlib.pyplot.
plotting
(
)
Function | Description |
| Plot the autocorrelation of |
| Plot the angle spectrum. |
| Annotate the point |
| Add an arrow to the axes. |
| Autoscale the axis view to the data (toggle). |
| Add an axes to the figure. |
| Add a horizontal line across the axis. |
| Add a horizontal span (rectangle) across the axis. |
| Convenience method to get or set axis properties. |
| Add a vertical line across the axes. |
| Add a vertical span (rectangle) across the axes. |
| Make a bar plot. |
| Plot a 2-D field of barbs. |
| Make a horizontal bar plot. |
| Turn the axes box on or off. |
| Make a box and whisker plot. |
| Plot horizontal bars. |
| 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 contours. |
| Plot contours. |
| Plot the cross-spectral density. |
| Remove an axes from the current figure. |
| Redraw the current figure. |
| Plot an errorbar graph. |
| Plot identical parallel lines at the given positions. |
| Adds a non-resampled image to the figure. |
| Place a legend in the figure. |
| |
| Add text to figure. |
| Creates a new figure. |
| Plot filled polygons. |
| Make filled polygons between two curves. |
| Make filled polygons between two horizontal curves. |
| Find artist objects. |
| Get the current |
| Get a reference to the current figure. |
| Get the current colorable artist. |
| Return a list of existing figure labels. |
| Return a list of existing figure numbers. |
| Turn the axes grids on or off. |
| Make a hexagonal binning plot. |
| Plot a histogram. |
| Make a 2D histogram plot. |
| Plot horizontal lines at each |
| |
| Read an image from a file into an array. |
| Save an array as in image file. |
| Display an image on the axes. |
Install a repl display hook so that any stale figure are automatically redrawn when control is returned to the repl. | |
| Turn interactive mode off. |
| Turn interactive mode on. |
| |
| Return status of interactive mode. |
| Places a legend on the axes. |
| Control behavior of 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 current plot. |
| Display minor ticks on the current plot. |
| |
| Pause for interval seconds. |
| Create a pseudocolor plot of a 2-D array. |
| Plot a quadrilateral mesh. |
| Plot the phase spectrum. |
| Plot a pie chart. |
| Plot lines and/or markers to the |
| A plot with data that contains dates. |
| Plot the data in a file. |
| Make a polar plot. |
| Plot the power spectral density. |
| Plot a 2-D field of arrows. |
| Add a key to a quiver plot. |
| Set the current rc params. |
| Return a context manager for managing rc settings. |
| Restore the rc params from Matplotlib’s internal defaults. |
| Get or set the radial gridlines on a polar plot. |
| Save the current figure. |
| Set the current Axes instance to ax. |
| Make a scatter plot of |
| Set the current image. |
| Make a plot with log scaling on the x axis. |
| Make a plot with log scaling on the |
| Set the default colormap. |
| Set a property on an artist object. |
| Display a figure. |
| Plot a spectrogram. |
| Plot the sparsity pattern on a 2-D array. |
| Draws a stacked area plot. |
| Create a stem plot. |
| Make a step plot. |
| Draws streamlines of a vector flow. |
| Return a subplot axes positioned by the given grid definition. |
| Create a subplot in a grid. |
| Launch a subplot tool window for a figure. |
| Create a figure and a set of subplots This utility wrapper makes it convenient to create common layouts of subplots, including the enclosing figure object, in a single call. |
Tune the subplot layout. | |
| Add a centered title to the figure. |
| Switch the default backend. |
| Add a table to the current axes. |
| Add text to the axes. |
| Get or set the theta locations of the gridlines in a polar plot. |
| Change the appearance of ticks and tick labels. |
Change the | |
| Automatically adjust subplot parameters to give specified padding. |
| Set a title of the current axes. |
| Draw contours on an unstructured triangular grid. |
| Draw contours on an unstructured triangular grid. |
| Create a pseudocolor plot of an unstructured triangular grid. |
| Draw a unstructured triangular grid as lines and/or markers. |
| Make a second axes that shares the x-axis. |
| Make a second axes that shares the y-axis. |
Uninstalls the matplotlib display hook. | |
| Make a violin plot. |
| Plot vertical lines. |
| Plot the cross correlation between x and y. |
| Turns on xkcd sketch-style drawing mode. |
| Set the x axis label of the current axis. |
| Get or set the x limits of the current axes. |
| Set the scaling of the x-axis. |
| Get or set the x-limits of the current tick locations and labels. |
| Set the y axis label of the current axis. |
| Get or set the y-limits of the current axes. |
| Set the scaling of the y-axis. |
| Get or set the y-limits of the current tick locations and labels. |
The Object-Oriented API
Most of these functions also exist as methods in the matplotlib.axes.Axes
class. You can use them with the “Object Oriented” approach to Matplotlib.While it is easy to quickly generate plots with the matplotlib.pyplot
module, we recommend using the object-oriented approach for more control and customization of your plots. See the methods in the matplotlib.axes.Axes()
class for many of the same plotting functions. For examples of the OO approach to Matplotlib, see the API Examples.
Colors in Matplotlib
There are many colormaps you can use to map data onto color values. Below we list several ways in which color can be utilized in Matplotlib.
For a more in-depth look at colormaps, see the Colormaps in Matplotlib tutorial.
matplotlib.pyplot.
colormaps
(
)
Matplotlib provides a number of colormaps, and others can be added using register_cmap()
. This function documents the built-in colormaps, and will also return a list of all registered colormaps if called.
You can set the colormap for an image, pcolor, scatter, etc, using a keyword argument:
imshow(X, cmap=cm.hot)
or using the set_cmap()
function:
imshow(X)
pyplot.set_cmap('hot')
pyplot.set_cmap('jet')
In interactive mode, set_cmap()
will update the colormap post-hoc, allowing you to see which one works best for your data.All built-in colormaps can be reversed by appending _r
: For instance, gray_r
is the reverse of gray
.
There are several common color schemes used in visualization:
Sequential schemes
for unipolar data that progresses from low to high
Diverging schemes
for bipolar data that emphasizes positive or negative deviations from a central value
Cyclic schemes
meant for plotting values that wrap around at the endpoints, such as phase angle, wind direction, or time of day
Qualitative schemes
for nominal data that has no inherent ordering, where color is used only to distinguish categories
Matplotlib ships with 4 perceptually uniform color maps which are the recommended color maps for sequential data:
Colormap
Description
inferno
perceptually uniform shades of black-red-yellow
magma
perceptually uniform shades of black-red-white
plasma
perceptually uniform shades of blue-red-yellow
viridis
perceptually uniform shades of blue-green-yellow
The following colormaps are based on the ColorBrewer color specifications and designs developed by Cynthia Brewer:
ColorBrewer Diverging (luminance is highest at the midpoint, and decreases towards differently-colored endpoints):
Colormap
Description
BrBG
brown, white, blue-green
PiYG
pink, white, yellow-green
PRGn
purple, white, green
PuOr
orange, white, purple
RdBu
red, white, blue
RdGy
red, white, gray
RdYlBu
red, yellow, blue
RdYlGn
red, yellow, green
Spectral
red, orange, yellow, green, blue
ColorBrewer Sequential (luminance decreases monotonically):
Colormap
Description
Blues
white to dark blue
BuGn
white, light blue, dark green
BuPu
white, light blue, dark purple
GnBu
white, light green, dark blue
Greens
white to dark green
Greys
white to black (not linear)
Oranges
white, orange, dark brown
OrRd
white, orange, dark red
PuBu
white, light purple, dark blue
PuBuGn
white, light purple, dark green
PuRd
white, light purple, dark red
Purples
white to dark purple
RdPu
white, pink, dark purple
Reds
white to dark red
YlGn
light yellow, dark green
YlGnBu
light yellow, light green, dark blue
YlOrBr
light yellow, orange, dark brown
YlOrRd
light yellow, orange, dark red
ColorBrewer Qualitative:
(For plotting nominal data, ListedColormap
is used, not LinearSegmentedColormap
. Different sets of colors are recommended for different numbers of categories.)
- Accent
- Dark2
- Paired
- Pastel1
- Pastel2
- Set1
- Set2
- Set3
A set of colormaps derived from those of the same name provided with Matlab are also included:
Colormap
Description
autumn
sequential linearly-increasing shades of red-orange-yellow
bone
sequential increasing black-white color map with a tinge of blue, to emulate X-ray film
cool
linearly-decreasing shades of cyan-magenta
copper
sequential increasing shades of black-copper
flag
repetitive red-white-blue-black pattern (not cyclic at endpoints)
gray
sequential linearly-increasing black-to-white grayscale
hot
sequential black-red-yellow-white, to emulate blackbody radiation from an object at increasing temperatures
hsv
cyclic red-yellow-green-cyan-blue-magenta-red, formed by changing the hue component in the HSV color space
jet
a spectral map with dark endpoints, blue-cyan-yellow-red; based on a fluid-jet simulation by NCSA [1]
pink
sequential increasing pastel black-pink-white, meant for sepia tone colorization of photographs
prism
repetitive red-yellow-green-blue-purple-…-green pattern (not cyclic at endpoints)
spring
linearly-increasing shades of magenta-yellow
summer
sequential linearly-increasing shades of green-yellow
winter
linearly-increasing shades of blue-green
A set of palettes from the Yorick scientific visualisation package, an evolution of the GIST package, both by David H. Munro are included:
Colormap
Description
gist_earth
mapmaker’s colors from dark blue deep ocean to green lowlands to brown highlands to white mountains
gist_heat
sequential increasing black-red-orange-white, to emulate blackbody radiation from an iron bar as it grows hotter
gist_ncar
pseudo-spectral black-blue-green-yellow-red-purple-white colormap from National Center for Atmospheric Research [2]
gist_rainbow
runs through the colors in spectral order from red to violet at full saturation (like hsv but not cyclic)
gist_stern
“Stern special” color table from Interactive Data Language software
Other miscellaneous schemes:
Colormap
Description
afmhot
sequential black-orange-yellow-white blackbody spectrum, commonly used in atomic force microscopy
brg
blue-red-green
bwr
diverging blue-white-red
coolwarm
diverging blue-gray-red, meant to avoid issues with 3D shading, color blindness, and ordering of colors [3]
CMRmap
“Default colormaps on color images often reproduce to confusing grayscale images. The proposed colormap maintains an aesthetically pleasing color image that automatically reproduces to a monotonic grayscale with discrete, quantifiable saturation levels.” [4]
cubehelix
Unlike most other color schemes cubehelix was designed by D.A. Green to be monotonically increasing in terms of perceived brightness. Also, when printed on a black and white postscript printer, the scheme results in a greyscale with monotonically increasing brightness. This color scheme is named cubehelix because the r,g,b values produced can be visualised as a squashed helix around the diagonal in the r,g,b color cube.
gnuplot
gnuplot’s traditional pm3d scheme (black-blue-red-yellow)
gnuplot2
sequential color printable as gray (black-blue-violet-yellow-white)
ocean
green-blue-white
rainbow
spectral purple-blue-green-yellow-orange-red colormap with diverging luminance
seismic
diverging blue-white-red
nipy_spectral
black-purple-blue-green-yellow-red-white spectrum, originally from the Neuroimaging in Python project
terrain
mapmaker’s colors, blue-green-yellow-brown-white, originally from IGOR Pro
The following colormaps are redundant and may be removed in future versions. It’s recommended to use the names in the descriptions instead, which produce identical output:
Colormap
Description
gist_gray
identical to gray
gist_yarg
identical to gray_r
binary
identical to gray_r
spectral
identical to nipy_spectral [5]