# Turning on inline plots -- just for use in ipython notebooks.
%pylab inline
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.
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.
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 (more on that later).
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.
A newer community resource is StackOverflow, so if you need to build up karma points, submit your questions here, and help others out too!
Matplotlib is hosted by GitHub.
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).
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()
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 statement you will ever need for matplotlib
import matplotlib.pyplot as plt
The pyplot module is where everything in matplotlib comes together. It is the launching point for preparing your figures, making plots, and doing any modifications and decorations you want. It all starts here. Let us take a look at those three catagories of pyplot functions.
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.
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 pylab-inline to display anything
plt.show()
fig = plt.figure(figsize=plt.figaspect(2.0)) # Twice as tall
fig.gca()
plt.show()
Open the file "plot_demo.svg" included with this notebook in a new tab.
All plotting is done with respect to an Axes. An Axes is made up of Axis objects and many other things. An Axes object must belong to a Figure (and only one Figure). Most commands you will ever issue will be with respect to this Axes object.
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.
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()
How would you make a plot with a y-axis such that it starts at 1000 at the bottom, and goes to 500 at the top?
fig, ax = plt.subplots(1, 1)
ax.set_ylim( )
plt.show()
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()
This is a constant source of confusion:
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"
plt.show()
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()
and fig.add_subplot()
Which should be familiar to all Matlab usersplt.axes()
plt.subplot2grid()
plt.subplot_tool()
Interactive modification of subplot 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.
# Without "tight_layout"
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.show()
# With "tight_layout"
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.
Under the hood, matplotlib utilizes GridSpec to layout 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.
There will be times when you want to have the x axis and/or the y axis of your subplots to be "twined" or "shared". Twining 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 twinned axis. 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 twinned axis will pan and zoom automatically.
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()
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1, 2, 3, 4], [10, 20, 25, 30])
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
ax.spines['bottom'].set_position(('outward', 10))
ax.spines['left'].set_position(('outward', 10))
plt.show()
Modify the above example to have spines and tick labels on both sides, with tickmarks through the spines.
While not required for plotting, 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.
import numpy as np
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.