# Sample plots in Matplotlib
Here you'll find a host of example plots with the code that generated them.
# Line Plot
Here's how to create a line plot with text labels using
plot() (opens new window).
# Multiple subplots in one figure
Multiple axes (i.e. subplots) are created with the
subplot() (opens new window) function:
Matplotlib can display images (assuming equally spaced
horizontal dimensions) using the
imshow() (opens new window) function.
Example of using
imshow() (opens new window) to display a CT scan
# Contouring and pseudocolor
pcolormesh() (opens new window) function can make a colored
representation of a two-dimensional array, even if the horizontal dimensions
are unevenly spaced. The
contour() (opens new window) function is another way to represent
the same data:
hist() (opens new window) function automatically generates
histograms and returns the bin counts or probabilities:
You can add arbitrary paths in Matplotlib using the
matplotlib.path (opens new window) module:
# Three-dimensional plotting
Thanks to John Porter, Jonathon Taylor, Reinier Heeres, and Ben Root for
mplot3d toolkit. This toolkit is included with all standard Matplotlib
streamplot() (opens new window) function plots the streamlines of
a vector field. In addition to simply plotting the streamlines, it allows you
to map the colors and/or line widths of streamlines to a separate parameter,
such as the speed or local intensity of the vector field.
Streamplot with various plotting options.
This feature complements the
quiver() (opens new window) function for
plotting vector fields. Thanks to Tom Flannaghan and Tony Yu for adding the
In support of the Phoenix (opens new window)
mission to Mars (which used Matplotlib to display ground tracking of
spacecraft), Michael Droettboom built on work by Charlie Moad to provide
an extremely accurate 8-spline approximation to elliptical arcs (see
Arc (opens new window)), which are insensitive to zoom level.
# Bar charts
bar() (opens new window) function to make bar charts, which
includes customizations such as error bars:
# Pie charts
pie() (opens new window) function allows you to create pie
charts. Optional features include auto-labeling the percentage of area,
exploding one or more wedges from the center of the pie, and a shadow effect.
Take a close look at the attached code, which generates this figure in just
a few lines of code.
table() (opens new window) function adds a text table
to an axes.
# Scatter plots
scatter() (opens new window) function makes a scatter plot
with (optional) size and color arguments. This example plots changes
in Google's stock price, with marker sizes reflecting the
trading volume and colors varying with time. Here, the
alpha attribute is used to make semitransparent circle markers.
# GUI widgets
Matplotlib has basic GUI widgets that are independent of the graphical
user interface you are using, allowing you to write cross GUI figures
and widgets. See
matplotlib.widgets (opens new window) and the
widget examples (opens new window).
Slider and radio-button GUI.
# Filled curves
fill() (opens new window) function lets you
plot filled curves and polygons:
Thanks to Andrew Straw for adding this function.
# Date handling
You can plot timeseries data with major and minor ticks and custom tick formatters for both.
# Log plots
Thanks to Andrew Straw, Darren Dale and Gregory Lielens for contributions log-scaling infrastructure.
# Polar plots
polar() (opens new window) function generates polar plots.
legend() (opens new window) function automatically
generates figure legends, with MATLAB-compatible legend-placement
Thanks to Charles Twardy for input on the legend function.
# TeX-notation for text objects
Below is a sampling of the many TeX expressions now supported by Matplotlib's
internal mathtext engine. The mathtext module provides TeX style mathematical
expressions using FreeType (opens new window)
and the DejaVu, BaKoMa computer modern, or STIX (opens new window)
fonts. See the
matplotlib.mathtext (opens new window) module for additional details.
Matplotlib's mathtext infrastructure is an independent implementation and does not require TeX or any external packages installed on your computer. See the tutorial at Writing mathematical expressions (opens new window).
# Native TeX rendering
Although Matplotlib's internal math rendering engine is quite powerful, sometimes you need TeX. Matplotlib supports external TeX rendering of strings with the usetex option.
# EEG GUI
You can embed Matplotlib into pygtk, wx, Tk, or Qt applications. Here is a screenshot of an EEG viewer called pbrain (opens new window).
The lower axes uses
specgram() (opens new window)
to plot the spectrogram of one of the EEG channels.
For examples of how to embed Matplotlib in different toolkits, see:
- Embedding in GTK3 (opens new window)
- Embedding in wx #2 (opens new window)
- Matplotlib With Glade 3 (opens new window)
- Embedding in Qt (opens new window)
- Embedding in Tk (opens new window)
# XKCD-style sketch plots
Just for fun, Matplotlib supports plotting in the style of
# Subplot example
Many plot types can be combined in one figure to create powerful and flexible representations of data.
import matplotlib.pyplot as plt import numpy as np np.random.seed(19680801) data = np.random.randn(2, 100) fig, axs = plt.subplots(2, 2, figsize=(5, 5)) axs[0, 0].hist(data) axs[1, 0].scatter(data, data) axs[0, 1].plot(data, data) axs[1, 1].hist2d(data, data) plt.show()