# Turning on inline plots -- just for use in ipython notebooks.
%matplotlib inline
Matplotlib¶
Introduction¶
Matplotlib is a library for producing publication-quality figures. mpl (for short) was designed from the bottom-up to serve dual-purposes. First, to allow for interactive, cross-platform control of figures and plots, and second, to make it very easy to produce static raster or vector graphics files without the need for any GUIs. Furthermore, mpl -- much like Python itself -- gives the developer complete control over the appearance of their plots, while still being very usable through a powerful defaults system.
Online Documentation¶
The matplotlib.org project website is the primary online resource for the library's documentation. It contains examples, FAQs, API documentation, and, most importantly, the gallery.
Gallery¶
Many users of matplotlib are often faced with the question, "I want to make a plot that has X with Y in the same figure, but it needs to look like Z". Good luck getting an answer from a web search with that query. This is why the gallery is so useful, because it showcases the variety of ways one can make plots. Browse through the gallery, click on any figure that has pieces of what you want to see the code that generated it. Soon enough, you will be like a chef, mixing and matching components to produce your masterpiece!
As always, if you have a new and interesting plot that demonstrates a feature of matplotlib, feel free to submit a well-commented version of the example code for inclusion.
Mailing Lists and StackOverflow¶
When you are just simply stuck, and can not figure out how to get something to work, or just need some hints on how to get started, you will find much of the community at the matplotlib-users mailing list. This mailing list is an excellent resource of information with many friendly members who just love to help out newcomers. The number one rule to remember with this list is to be persistant. While many questions do get answered fairly quickly, some do fall through the cracks, or the one person who knows the answer isn't available. Therefore, try again with your questions rephrased, or with a plot showing your attempts so far. We love plots, so an image showing what is wrong often gets the quickest responses.
Another community resource is StackOverflow, so if you need to build up karma points, submit your questions here, and help others out too!
Github repository¶
Location¶
Matplotlib is hosted by GitHub.
Bug Reports and feature requests¶
So, you think you found a bug? Or maybe you think some feature is just too difficult to use? Or missing altogether? Submit your bug reports here at matplotlib's issue tracker. We even have a process for submitting and discussing Matplotlib Enhancement Proposals (MEPs).
What is this "backend" thing I keep hearing about?¶
Matplotlib has multiple backends. The backends allow mpl to be used on a variety of platforms with a variety of GUI toolkits (GTK, Qt, Wx, etc.), all of them written so that most of the time, you will not need to care which backend you are using. However, bugs do occur, and so two of the most important pieces of information you can provide in a bug report is which version of matplotlib, and which backend.
import matplotlib
print matplotlib.__version__
print matplotlib.get_backend()
On with the show!¶
Matplotlib is a large project and can seem daunting at first. However, by learning the components, it should begin to feel much smaller and more approachable.
We start with the most important import statements you will ever need for matplotlib
import numpy as np
import matplotlib.pyplot as plt
pyplot¶
The pyplot module is where everything in matplotlib comes together. It is the launching point for (1) preparing your figures, (2) making plots, and (3) doing any modifications and decorations you want. It all comes together here. Let us take a look at those three catagories of pyplot functions.
Plotting Preparation¶
| Function | Description |
|---|---|
autoscale |
Autoscale the axis view to the data (toggle). |
axes |
Add an axes to the figure. |
axis |
Convenience method to get or set axis properties. |
cla |
Clear the current axes. |
clf |
Clear the current figure. |
clim |
Set the color limits of the current image. |
delaxes |
Remove an axes from the current figure. |
locator_params |
Control behavior of tick locators. |
margins |
Set or retrieve autoscaling margins. |
figure |
Creates a new figure. |
gca |
Return the current axis instance. |
gcf |
Return a reference to the current figure. |
gci |
Get the current colorable artist. |
hold |
Set the hold state. |
ioff |
Turn interactive mode off. |
ion |
Turn interactive mode on. |
ishold |
Return the hold status of the current axes. |
isinteractive |
Return status of interactive mode. |
rc |
Set the current rc params. |
rc_context |
Return a context manager for managing rc settings. |
rcdefaults |
Restore the default rc params. |
savefig |
Save the current figure. |
sca |
Set the current Axes instance. |
sci |
Set the current image. |
set_cmap |
Set the default colormap |
setp |
Set a property on an artist object |
show |
Display a figure |
subplot |
Return a subplot axes positioned by the given grid definition. |
subplot2grid |
Create a subplot in a grid. |
subplot_tool |
Launch a subplot tool window for a figure. |
subplots |
Create a figure with a set of subplots already made. |
subplots_adjust |
Tune the subplot layout. |
switch_backend |
Switch the default backend. |
tick_params |
Change the appearance of ticks and tick labels. |
ticklabel_format |
Change the ScalarFormatter used by default for linear axes. |
tight_layout |
Automatically adjust subplot parameters to give specified padding. |
xkcd |
Turns on XKCD sketch-style drawing mode. |
xlabel |
Set the x axis label of the current axis. |
xlim |
Get or set the x limits of the current axes. |
xscale |
Set the scaling of the x-axis. |
xticks |
Get or set the x-limits of the current tick locations and labels. |
ylabel |
Set the y axis label of the current axis. |
ylim |
Get or set the y-limits of the current axes. |
yscale |
Set the scaling of the y-axis. |
yticks |
Get or set the y-limits of the current tick locations and labels. |
Plotting Functions¶
| Function | Description |
|---|---|
acorr |
Plot the autocorrelation of x |
bar |
Make a bar plot |
barbs |
Plot a 2-D field of barbs |
barh |
Make a horizontal bar plot |
boxplot |
Make a box and whisker plot |
broken_barh |
Plot horizontal bars |
cohere |
Plot the coherence between x and y |
contour |
Plot contours |
contourf |
Plot filled contours |
csd |
Plot cross-spectral density |
errorbar |
Plot an errorbar graph |
eventplot |
Plot identical parallel lines at specific positions |
fill |
Plot filled polygons |
fill_between |
Make filled polygons between two curves |
fill_betweenx |
Make filled polygons between two horizontal curves |
hexbin |
Make a hexagonal binning plot |
hist |
Plot a histogram |
hist2d |
Make a 2D histogram plot |
imshow |
Display an image on the axes |
loglog |
Make a plot with log scaling on both the x and y axis |
matshow |
Display an array as a matrix in a new figure window |
pcolor |
Create a pseudocolor plot of a 2-D array |
pcolormesh |
Plot a quadrilateral mesh |
pie |
Plot a pie chart |
plot |
Plot lines and/or markers |
plot_date |
Plot with data with dates |
polar |
Make a polar plot |
psd |
Plot the power spectral density |
quiver |
Plot a 2-D field of arrows |
scatter |
Make a scatter plot of x vs y |
semilogx |
Make a plot with log scaling on the x axis |
semilogy |
Make a plot with log scaling on the y axis |
specgram |
Plot a spectrogram |
spy |
Plot the sparsity pattern on a 2-D array |
stackplot |
Draws a stacked area plot |
stem |
Create a stem plot |
step |
Make a step plot |
streamplot |
Draws streamlines of a vector flow |
tricontour |
Draw contours on an unstructured triangular grid |
tricontourf |
Draw filled contours on an unstructured triangular grid |
tripcolor |
Create a pseudocolor plot of an unstructured triangular grid |
triplot |
Draw a unstructured triangular grid as lines and/or markers |
xcorr |
Plot the cross-correlation between x and y |
Plot modifiers¶
| Function | Description |
|---|---|
annotate |
Create an annotation: a piece of text referring to a data point |
arrow |
Add an arrow to the axes |
axhline |
Add a horizontal line across the axis |
axhspan |
Add a horizontal span (rectangle) across the axis |
axvline |
Add a vertical line across the axes |
axvspan |
Add a vertical span (rectangle) across the axes |
box |
Turn the axes box on or off |
clabel |
Label a contour plot |
colorbar |
Add a colorbar to a plot |
grid |
Turn the axes grids on or off |
hlines |
Plot horizontal lines |
legend |
Place a legend on the current axes |
minorticks_off |
Remove minor ticks from the current plot |
minorticks_on |
Display minor ticks on the current plot |
quiverkey |
Add a key to a quiver plot |
rgrids |
Get or set the radial gridlines on a polar plot |
suptitle |
Add a centered title to the figure |
table |
Add a table to the current axes |
text |
Add text to the axes |
title |
Set a title of the current axes |
vlines |
Plot vertical lines |
xlabel |
Set the x axis label of the current axis |
ylabel |
Set the y axis label of the current axis |
fig = plt.figure()
Awww, nothing happened! This is because by default mpl will not show anything until told to do so. In other words, the "interactive mode" is turned off. This is very useful for scripting where we would not ever want to see the intermediate results. For those who wishes to experiment and want to see their plot as they issue commands, there is the "plt.ion()" command they can issue before creating their first figure of their session. For the purpoase of this tutorial, we will leave interactivity off.
Figure properties¶
figsize : tuple of integers, width, height in inches.
dpi : integer, esolution of the figure in dots per inch.
fig = plt.figure(figsize=(10, 4))
fig.gca() # needed for the ipython-inline to display anything
plt.show()
A really useful utility function is figaspect
# Twice as tall
fig = plt.figure(figsize=plt.figaspect(2.0))
fig.gca()
plt.show()
Breakdown of a figure¶
Open the file "plot_demo.svg" included with this notebook in a new tab.
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1, 2, 3, 4], [10, 20, 25, 30])
ax.set_xlim(0.5, 4.5)
plt.show()
Interestingly, just about all methods of an Axes object exist as a function in the pyplot module (and vice-versa). For example, when calling plt.xlim(1, 10), pyplot calls ax.set_xlim(1, 10) on whichever Axes is "current". Here is an equivalent version of the above example using just pyplot.
plt.plot([1, 2, 3, 4], [10, 20, 25, 30])
plt.xlim(0.5, 4.5)
plt.show()
Much cleaner, and much clearer! So, why will most of my examples not follow the pyplot approach? Because PEP20 "The Zen of Python" says:
"Explicit is better than implicit"
While very simple plots, with short scripts would benefit from the conciseness of the pyplot implicit approach, when doing more complicated plots, or working within larger scripts, you will want to explicitly pass around the Axes and/or Figure object to operate upon.
Limits and autoscaling¶
By default, matplotlib will attempt to determine limits for you that encompasses all the data you have plotted. This is the "autoscale" feature. For line and image plots, the limits are not padded, while plots such as scatter plots and bar plots are given some padding.
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=plt.figaspect(0.5))
ax1.plot([-10, -5, 0, 5, 10, 15], [-1.2, 2, 3.5, -0.3, -4, 1])
ax2.scatter([-10, -5, 0, 5, 10, 15], [-1.2, 2, 3.5, -0.3, -4, 1])
plt.show()
A trick with limits is to specify only half of a limit. When done after a plot is made, this has the effect of allowing the user to anchor a limit while letting matplotlib to autoscale the rest of it.
# Good -- setting limits after plotting is done
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=plt.figaspect(0.5))
ax1.plot([-10, -5, 0, 5, 10, 15], [-1.2, 2, 3.5, -0.3, -4, 1])
ax2.scatter([-10, -5, 0, 5, 10, 15], [-1.2, 2, 3.5, -0.3, -4, 1])
ax1.set_ylim(bottom=-10)
ax2.set_xlim(right=25)
plt.show()
# Bad -- Setting limits before plotting is done
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=plt.figaspect(0.5))
ax1.set_ylim(bottom=-10)
ax2.set_xlim(right=25)
ax1.plot([-10, -5, 0, 5, 10, 15], [-1.2, 2, 3.5, -0.3, -4, 1])
ax2.scatter([-10, -5, 0, 5, 10, 15], [-1.2, 2, 3.5, -0.3, -4, 1])
plt.show()
%load exercises/1.1-limits.py
Labels and Legends¶
You can label just about anything in mpl. You can provide a label to your plot, which allows your legend to automatically build itself. The X and Y axis can also be labeled, as well as the subplot itself via the title.
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1, 2, 3, 4], [10, 20, 25, 30], label='Philadelphia')
ax.plot([1, 2, 3, 4], [30, 23, 13, 4], label='Boston')
ax.set_ylabel('Temperature (deg C)')
ax.set_xlabel('Time')
ax.set_title("A tale of two cities")
ax.legend()
plt.show()
Also, if you happen to be plotting something that you do not want to appear in the legend, just set the label to "_nolegend_".
fig, ax = plt.subplots(1, 1)
ax.bar([1, 2, 3, 4], [10, 20, 25, 30], label="Foobar")
ax.plot([1, 2, 3, 4], [10, 20, 25, 30], label="_nolegend_")
ax.legend()
plt.show()
Ticks, Tick Lines, Tick Labels and Tickers¶
This is a constant source of confusion:
- A Tick is the location of a Tick Label.
- A Tick Line is the line that denotes the location of the tick.
- A Tick Label is the text that is displayed at that tick.
- A
Tickerautomatically determines the ticks for an Axis and formats the tick labels.
tick_params() is often used to help configure your tickers.
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1, 2, 3, 4], [10, 20, 25, 30])
ax.xaxis.set_ticks(range(1, 5)) # Set ticks at 1, 2, 3, 4
ax.xaxis.set_ticklabels([3, 100, -12, "foo"]) # Label ticks as "3", "100", "-12", and "foo"
ax.tick_params(axis='y', direction='inout', length=10)
plt.show()
Subplots¶
While an Axes object can only belong to one Figure, A Figure can have many Axes objects. These are typically called "subaxes" or "subplots". They act just like regular Axes.
fig = plt.figure(figsize=(10, 5))
ax = fig.add_subplot(121)
ax.plot([1, 2, 3, 4], [10, 20, 25, 30], label='Philadelphia')
ax.plot([1, 2, 3, 4], [30, 23, 13, 4], label='Boston')
ax.set_title('A tale of two cities')
ax.legend()
t = np.linspace(0, 7, 25)
z = 2 * np.sin(t) + 5
ax = fig.add_subplot(122)
ax.scatter(t, z, label='Philadelphia')
ax.set_title("Observed Tide")
ax.legend()
fig.suptitle('A title for the whole figure')
plt.show()
There are many ways to add and modify subplots in a figure.
plt.subplots()plt.subplot()andfig.add_subplot()Which should be familiar to all Matlab usersplt.axes()plt.subplot2grid()plt.subplot_tool()Interactive modification of subplot spacing.
Spacing¶
The spacing between the subplots can be adjusted using plt.subplots_adjust(). Play around with the example below to see how the different arguments affect the spacing.
fig, axes = plt.subplots(2, 2, figsize=(9, 9))
plt.subplots_adjust(wspace=0.5, hspace=0.3,
left=0.125, right=0.9,
top=0.9, bottom=0.1)
plt.show()
A common complaint with matplotlib users is that the labels do not fit with the subplots, or the label of one subplot spills onto another subplot's area. Matplotlib does not currently have any sort of robust layout engine, as it is a design decision to minimize the amount of "magic" that matplotlib performs. We intend to let users have complete, 100% control over their plots. LaTeX users would be quite familiar with the amount of frustration that can occur with placement of figures in their documents.
That said, there have been some efforts to develop tools that users can use to help address the most common compaints. The "Tight Layout" feature, when invoked, will attempt to resize margins, and subplots so that nothing overlaps.
def example_plot(ax):
ax.plot([1, 2])
ax.set_xlabel('x-label', fontsize=16)
ax.set_ylabel('y-label', fontsize=8)
ax.set_title('Title', fontsize=24)
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)
example_plot(ax1)
example_plot(ax2)
example_plot(ax3)
example_plot(ax4)
#plt.tight_layout()
plt.show()
As a last bit of an FAQ, this "tight_layout" feature is unrelated to the so-called "bbox_inches='tight'" feature that will be discussed separately.
GridSpec¶
Under the hood, matplotlib utilizes GridSpec to lay out the subplots. While plt.subplots() is fine for simple cases, sometimes you will need more advanced subplot layouts. In such cases, you should use GridSpec directly. GridSpec is outside the scope of this tutorial, but it is handy to know that it exists. Here is a guide on how to use it.
Sharing axes¶
There will be times when you want to have the x axis and/or the y axis of your subplots to be "shared". Sharing an axis means that the axis in one or more subplots will be tied together such that any change in one of the axis changes all of the other shared axes. This works very nicely with autoscaling arbitrary datasets that may have overlapping domains. Furthermore, when interacting with the plots (panning and zooming), all of the shared axes will pan and zoom automatically.
# going out of inline mode to demonstrate the zooming and panning
%matplotlib
fig, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=True)
ax1.plot([1, 2, 3, 4], [1, 2, 3, 4])
ax2.plot([3, 4, 5, 6], [6, 5, 4, 3])
plt.show()
# Go back to inline mode
%matplotlib inline
"Twinning" axes¶
Sometimes one may want to overlay two plots on the same axes, but the scales may be entirely different. You can simply treat them as separate plots, but then twin them.
fig, ax1 = plt.subplots(1, 1)
ax1.plot([1, 2, 3, 4], [1, 2, 3, 4])
ax2 = ax1.twinx()
ax2.scatter([1, 2, 3, 4], [60, 50, 40, 30])
ax1.set_xlabel('X')
ax1.set_ylabel('First scale')
ax2.set_ylabel('Other scale')
plt.show()
Axis Spines¶
Spines are the axis lines for a plot. Each plot can have four spines: "top", "bottom", "left" and "right". By default, they are set so that they frame the plot, but they can be individually positioned and configured via the set_position() method of the spine. Here are some different configurations.
fig, ax = plt.subplots(1, 1)
ax.plot([-2, 2, 3, 4], [-10, 20, 25, 5])
ax.spines['top'].set_visible(False)
ax.xaxis.set_ticks_position('bottom') # no ticklines at the top
ax.spines['right'].set_visible(False)
ax.yaxis.set_ticks_position('left') # no ticklines on the right
# "outward"
# Move the two remaining spines "out" away from the plot by 10 points
ax.spines['bottom'].set_position(('outward', 10))
ax.spines['left'].set_position(('outward', 10))
# "data"
# Have the spines stay intersected at (0,0)
#ax.spines['bottom'].set_position(('data', 0))
#ax.spines['left'].set_position(('data', 0))
# "axes"
# Have the two remaining spines placed at a fraction of the axes
#ax.spines['bottom'].set_position(('axes', 0.75))
#ax.spines['left'].set_position(('axes', 0.25))
plt.show()
Exercise 1.2¶
Create a plot that have outward spines on the left and bottom with tick labels, and "centered" spines with no tick labels and tickmarks through the spines.
Hints:
tick_params()
set_position()
%load exercises/1.2-spines.py
Colorbars¶
Colorbars are much like legends because they help to describe the data being displayed. While legends describe plots, i.e., plot(), scatter(), hist(), stem(), colorbars describe images. To be really specific and technical, they can be used for any "ScalarMappable", which will be discussed in the Artists section. Let us take a look at a very simple example of a colorbar for a simple 2D image.
y, x = np.ogrid[-6:6:20j, -10:10:30j]
z = np.hypot(x, y)
plt.imshow(z)
plt.colorbar()
plt.show()
plt.imshow(z)
plt.colorbar(orientation='horizontal', shrink=0.75) # We can make colorbars do all sorts of things!
plt.show()
plt.imshow(z)
cbar = plt.colorbar(extend='both', aspect=10)
cbar.set_label('distance') # And we can even add a label to it
plt.show()
Colorbars in matplotlib can be difficult at times, and the documentation can sometimes be a bit unhelpful (patches welcome!). One of the most common problems that come up is when mixing subplots with a single colorbar:
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.imshow(z)
im = ax2.imshow(z) # Note, due to a bug, you will need to save the
# returned image object when calling imshow() from an Axes
# and pass that to plt.colorbar() so that it knows what
# image to build a colorbar from. This will be fixed for v1.3.1.
plt.colorbar(im)
plt.show()
Looks terrible, right? What is happening is that a colorbar in matplotlib is just a very squashed subplot with an image of the colormap and axis ticks and labels. When told to create a colorbar for an image, matplotlib will simply "steal" space from that image's subplot and create a new subplot. There are a couple ways to deal with this issue. First, if you preallocate space for the colorbar (by creating your own Axes object to add to the Figure), you can pass that preallocated Axes to the "cax" argument of plt.colorbar(), and it won't have to steal any space. The easier option is to pass a list of all the axes objects to plt.colorbar(..., ax), and it will steal space equally from them.
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.imshow(z)
im = ax2.imshow(z)
plt.colorbar(im, ax=[ax1, ax2], shrink=0.5)
plt.show()
There is also a third, very powerful, option called axes_grid1, which we will discuss in the mpl_toolkits section.