Matplotlib Handout
Matplotlib Handout
author:
email:
version:
Contents
1 Introduction
2 IPython
3 pylab
4 Simple Plots
4.1 Exercises
5 Properties
5.1 Exercise
6 Text
10
6.1 Exercise
11
7 Ticks
11
11
11
12
7.4 Exercises
13
13
13
8.2 Figures
13
8.3 Subplots
14
8.4 Axes
15
8.5 Exercises
15
15
15
15
16
16
17
17
9.7 Histograms
18
19
19
20
20
21
21
22
22
23
23
24
24
10.4 Example
24
10.5 Exercises
25
25
11.1 Exercises
27
12 Animations
28
12.1 Exercises
30
1 Introduction
1 Introduction
matplotlib is probably the single most used Python package for 2D-graphics. It provides both a very quick
way to visualize data from Python and publication-quality figures in many formats. We are going to explore
matplotlib in interactive mode covering most common cases. We also look at the class library which is
provided with an object-oriented interface.
2 IPython
IPython is an enhanced interactive Python shell that has lots of interesting features including named inputs
and outputs, access to shell commands, improved debugging and many more. When we start it with the
command line argument -pylab, it allows interactive matplotlib
sessions that has
Matlab/Mathematica-like functionality.
3 pylab
pylab provides a procedural interface to the matplotlib object-oriented plotting library. It is modeled
closely after Matlab(TM). Therefore, the majority of plotting commands in pylab has Matlab(TM) analogs
with similar arguments. Important commands are explained with interactive examples.
4 Simple Plots
Let's start an interactive session:
$python ipython.py -pylab
This brings us to the IPython prompt:
IPython
?
%magic
help
object?
In [1]:
Now we can make our first, really simple plot:
In [1]: plot(range(10))
Out[1]: [<matplotlib.lines.Line2D instance at 0x01AA26E8>]
In [2]:
The numbers form 0 through 9 are plotted:
1 Introduction
1 Introduction
To apply the new properties we need to redraw the screen:
In [10]: draw()
We can also add several lines to one plot:
In [1]: x = arange(100)
In [2]: linear = arange(100)
In [3]: square = [v * v for v in arange(0, 10, 0.1)]
In [4]: lines = plot(x, linear, x, square)
Let's add a legend:
In [5]: legend(('linear', 'square'))
Out[5]: <matplotlib.legend.Legend instance at 0x01BBC170>
This does not look particularly nice. We would rather like to have it at the left. So we clean the old graph:
In [6]: clf()
and print it anew providing new line styles (a green dotted line with crosses for the linear and a red dashed
line with circles for the square graph):
In [7]: lines = plot(x, linear, 'g:+', x, square, 'r--o')
Now we add the legend at the upper left corner:
In [8]: l = legend(('linear', 'square'), loc='upper left')
The result looks like this:
4.1 Exercises
4.1 Exercises
1. Plot a simple graph of a sinus function in the range 0 to 3 with a step size of 0.01.
2. Make the line red. Add diamond-shaped markers with size of 5.
3. Add a legend and a grid to the plot.
5 Properties
So far we have used properties for the lines. There are three possibilities to set them:
1) as keyword arguments at creation time: plot(x, linear, 'g:+', x, square, 'r--o').
2. with the function setp: setp(line, color='g').
3. using the set_something methods: line.set_marker('o')
Lines have several properties as shown in the following table:
Property
Value
alpha
antialiased
color
data_clipping
label
linestyle
one of - : -. -
linewidth
marker
markeredgewidth
4.1 Exercises
markeredgecolor
markerfacecolor
markersize
There are many line styles that can be specified with symbols:
Symbol
Description
solid line
--
dashed line
-.
dash-dot line
dotted line
points
pixels
circle symbols
triangle up symbols
<
>
square symbols
plus symbols
cross symbols
diamond symbols
tripod up symbols
hexagon symbols
pentagon symbols
steps
Colors can be given in many ways: one-letter abbreviations, gray scale intensity from 0 to 1, RGB in hex and
tuple format as well as any legal html color name.
5.1 Exercise
The one-letter abbreviations are very handy for quick work. With following you can get quite a few things
done:
Abbreviation
Color
blue
green
red
cyan
magenta
yellow
black
white
Other objects also have properties. The following table list the text properties:
Property
Value
alpha
color
family
fontangle
horizontalalignment
multialignment
name
position
x,y location
variant
rotation
size
style
text
verticalalignment
weight
5.1 Exercise
1. Apply different line styles to a plot. Change line color and thickness as well as the size and the kind of
the marker. Experiment with different styles.
6 Text
6 Text
We've already used some commands to add text to our figure: xlabel ylabel, and title.
There are two functions to put text at a defined position. text adds the text with data coordinates:
In [2]: plot(arange(10))
In [3]: t1 = text(5, 5, 'Text in the middle')
figtext uses figure coordinates form 0 to 1:
In [4]: t2 = figtext(0.8, 0.8, 'Upper right text')
\pi
If you want to get more control over where the text goes, you use annotations:
In [4]: ax.annotate('Here is something special', xy = (1, 1))
We will write the text at the position (1, 1) in terms of data. There are many optional arguments that help to
customize the position of the text. The arguments textcoords and xycoords specifies what x and y
mean:
argument
coordinate system
'figure points'
'figure pixels'
'figure fraction'
'axes points'
'axes pixels'
6.1 Exercise
'axes fraction'
'data'
6.1 Exercise
1. Annotate a line at two places with text. Use green and red arrows and align it according to figure
points and data.
7 Ticks
7.1 Where and What
Well formated ticks are an important part of publishing-ready figures. matplotlib provides a totally
configurable system for ticks. There are tick locators to specify where ticks should appear and tick formatters
to make ticks look like the way you want. Major and minor ticks can be located and formated independently
from each other. Per default minor ticks are not shown, i.e. there is only an empty list for them because it is as
NullLocator (see below).
Description
NullLocator
no ticks
IndexLocator
LinearLocator
LogLocator
MultipleLocator
AutoLocator
Description
MinuteLocator
locate minutes
HourLocator
locate hours
DayLocator
WeekdayLocator
MonthLocator
YearLocator
RRuleLocator
Description
NullFormatter
FixedFormatter
FuncFormatter
FormatStrFormatter
IndexFormatter
ScalarFormatter
LogFormatter
DateFormatter
All of these formatters derive from the base class matplotlib.ticker.Formatter. You can make your
own formatter deriving from it.
Now we set our major locator to 2 and the minor locator to 1. We also format the numbers as decimals using
the FormatStrFormatter:
In [5]: major_locator = MultipleLocator(2)
In [6]: major_formatter = FormatStrFormatter('%5.2f')
In [7]: minor_locator = MultipleLocator(1)
In [8]: ax.xaxis.set_major_locator(major_locator)
In [9]: ax.xaxis.set_minor_locator(minor_locator)
7.4 Exercises
In [10]: ax.xaxis.set_major_formatter(major_formatter)
In [10]: draw()
After we redraw the figure our x axis should look like this:
7.4 Exercises
1. Plot a graph with dates for one year with daily values at the x axis using the built-in module datetime.
2. Format the dates in such a way that only the first day of the month is shown.
3. Display the dates with and without the year. Show the month as number and as first three letters of the
month name.
8.2 Figures
A figure is the windows in the GUI that has "Figure #" as title. Figures are numbered starting from 1 as
opposed to the normal Python way starting from 0. This is clearly MATLAB-style. There are several
parameters that determine how the figure looks like:
Argument
Default
Description
num
number of figure
figsize
figure.figsize
dpi
figure.dpi
facecolor
figure.facecolor
edgecolor
figure.edgecolor
frameon
True
8.3 Subplots
The defaults can be specified in the resource file and will be used most of the time. Only the number of the
figure is frequently changed.
When you work with the GUI you can close a figure by clicking on the x in the upper right corner. But you can
close a figure programmatically by calling close. Depending on the argument it closes (1) the current figure
(no argument), (2) a specific figure (figure number or figure instance as argument), or (3) all figures (all as
argument).
As with other objects, you can set figure properties also setp or with the set_something methods.
8.3 Subplots
With subplot you can arrange plots in regular grid. You need to specify the number of rows and columns
and the number of the plot.
A plot with two rows and one column is created with subplot(211) and subplot(212). The result looks
like this:
If you want two plots side by side, you create one row and two columns with subplot(121) and
subplot(112). The result looks like this:
You can arrange as many figures as you want. A two-by-two arrangement can be created with
subplot(221), subplot(222), subplot(223), and subplot(224). The result looks like this:
Frequently, you don't want all subplots to have ticks or labels. You can set the xticklabels or the
yticklabels to an empty list ([]). Every subplot defines the methods 'is_first_row, is_first_col,
8.4 Axes
is_last_row, is_last_col. These can help to set ticks and labels only for the outer pots.
8.4 Axes
Axes are very similar to subplots but allow placement of plots at any location in the figure. So if we want to put
a smaller plot inside a bigger one we do so with axes:
In [22]: plot(x)
Out[22]: [<matplotlib.lines.Line2D instance at 0x02C9CE90>]
In [23]: a = axes([0.2, 0.5, 0.25, 0.25])
In [24]: plot(x)
The result looks like this:
8.5 Exercises
1. Draw two figures, one 5 by 5, one 10 by 10 inches.
2. Add four subplots to one figure. Add labels and ticks only to the outermost axes.
3. Place a small plot in one bigger plot.
The default column width is 0.8. It can be changed with common methods by setting width. As it can be
color and bottom, we can also set an error bar with yerr or xerr.
The range of the whiskers can be determined with the argument whis, which defaults to 1.5. The range of the
whiskers is between the most extreme data point within whis*(75%-25%) of the data.
1.,
1.,
1.,
1.,
1.,
1.,
1.],
9.7 Histograms
[
[
[
[
[
[
[
[
[
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
3.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
7.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
4.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.,
1.],
1.],
1.],
1.],
1.],
1.],
1.],
1.],
1.]])
We can also fill the area. We just use numbers form 0 to 9 for the values v:
v = x
contourf(x, x, z, v)
Now our plot area is filled:
9.7 Histograms
We can make histograms. Let's get some normally distributed random numbers from numpy:
import numpy as N
r_numbers = N.random.normal(size= 1000)
Now we make a simple histogram:
hist(r_numbers)
With 100 numbers our figure looks pretty good:
If we want only one axis with a logarithmic scale we can use semilogx or semilogy.
quiver(x, y, u, v)
All arrows point to the upper right, except two. The one at the location (4, 4) has 3 units in x-direction and the
other at location (1, 1) has -1 unit in y direction:
[0,
[0,
[0,
[0,
[0,
[0,
[0,
[0,
0,
0,
0,
0,
0,
0,
0,
0,
1,
0,
0,
0,
0,
0,
0,
0,
0,
1,
0,
0,
0,
0,
0,
0,
0,
0,
1,
0,
0,
0,
0,
0,
0,
0,
0,
1,
0,
0,
0,
0,
0,
0,
0,
0,
1,
0,
0,
0,
0,
0,
0,
0,
0,
1,
0,
0,
0,
0,
0,
0,
0,
0,
1,
0,
0],
0],
0],
0],
0],
0],
0],
1]])
dates
[datetime.datetime(2000,
datetime.datetime(2000,
datetime.datetime(2000,
datetime.datetime(2000,
datetime.datetime(2000,
datetime.datetime(2000,
datetime.datetime(2000,
datetime.datetime(2000,
datetime.datetime(2000,
datetime.datetime(2000,
1,
1,
1,
2,
3,
3,
3,
4,
4,
5,
1, 0, 0),
16, 0, 0),
31, 0, 0),
15, 0, 0),
1, 0, 0),
16, 0, 0),
31, 0, 0),
15, 0, 0),
30, 0, 0),
15, 0, 0)]
10.4 Example
Let's look at an example for using the object-oriented API:
#file matplotlib/oo.py
import matplotlib.pyplot as plt
#1
figsize = (8, 5)
fig = plt.figure(figsize=figsize)
ax = fig.add_subplot(111)
line = ax.plot(range(10))[0]
ax.set_title('Plotted with OO interface')
ax.set_xlabel('measured')
ax.set_ylabel('calculated')
ax.grid(True)
line.set_marker('o')
#2
#3
#4
#5
#6
plt.savefig('oo.png', dpi=150)
plt.show()
#9
#10
#7
#8
Since we are not in the interactive pylab-mode, we need to import pyplot (#1). We set the size of our figure
to be 8 by 5 inches (#2). Now we initialize a new figure (#3) and add a subplot to the figure (#4). The 111
says one plot at position 1, 1 just as in MATLAB. We create a new plot with the numbers from 0 to 9 and at
the same time get a reference to our line (#5). We can add several things to our plot. So we set a title and
labels for the x and y axis (#6). We also want to see the grid (#7) and would like to have little filled circles as
markers (#8).
10.5 Exercises
Finally, we save our figure as a PNG file specifying the desired resolution in dpi (#9) and show our figure on
the screen (#10).
10.5 Exercises
1. Use the object-oriented API of matplotlib to create a png-file with a plot of two lines, one linear and
square with a legend in it.
#1
#2
#3
First we need to import matplotlib itself (#1) as well as pyplot (#2) and numpy (#3).
StackPlot(object):
"""A stacked plot.
#4
10.5 Exercises
ncolors = len(self.y_data)
colors = default_colors * (1 + (ncolors // len(default_colors)))
self.colors = colors[:ncolors]
self.stacked = None
self._stack_data()
Now we define a new class StackedPlot (#4). It takes the x values and a two dimensional array of y
values, where each row will become one plot, as well as a bunch of optional arguments that we will meet later
(#5). We convert all data into numpy arrays (#6). This won't hurt if they are arrays already. If no axes was
supplied, we just get the current axes (#7). This is consistent with the behavior of the standard plots. We
define some default colors that will be used if none are given in the __init__ (#8). We don't know how
many plots might be stacked. Therefore, we just repeat the default colors again and again until all data have a
color (#9).
def _stack_data(self):
#10
"""Accumulate data.
"""
nlines = self.y_data.shape[0] + 1
#11
self.stacked = numpy.zeros((nlines, self.y_data.shape[1]))
#12
for index in xrange(1, nlines):
self.stacked[index] = (self.stacked[index - 1] +
#13
self.y_data[index - 1])
We stack our data (#10). We need one more entry than y values (#11) and the first one is all zeros (#12).
Than we just add the new values on top of the ones below (#13).
def draw(self):
#14
"""Draw the plot.
"""
for data1, data2, color in zip(self.stacked[:-1], self.stacked[1:],
self.colors):
#15
self.axes.fill_between(self.x, data1, data2, color=color) #16
self.axes.plot(self.x, data2, 'k', linewidth=0.1)
#17
if self.names:
rects = []
for color in self.colors:
#18
rects.append(plt.Rectangle((0, 0), 1, 1, fc=color))
self.axes.legend(rects, self.names, loc=self.loc,
prop=mpl.font_manager.FontProperties(size=10))
We draw our new plot (#14). Using zip, we go through all stacked data each time with a lower and upper
boundary as well as through our colors (#15). We fill the space between both plots (#16) and also plot the
line in black (#17). Furthermore, we make a nice legend for all colors (#18).
def __getattr__(self, name):
"""Delegate not found attributes to axes.
This works for set_tile, set_xlabel etc.
#19
11.1 Exercises
"""
try:
return getattr(self.axes, name)
except AttributeError:
raise AttributeError("'StackPlot' object has no attribute '%s'"
% name)
We delegate all attributes that are not found (#19) to or axes. This allows to provide all the
set_<property> methods without actually implementing them.
if __name__ == '__main__':
def test():
"""Check if it works.
"""
x = range(10)
y_data = numpy.ones((5, 10), dtype=numpy.float)
y_data[1, 1] = 2
y_data[2, 1] = 2
y_data[3, 1] = 2
y_data[1, 2] = 0.5
y_data[2, 3] = 0.5
y_data[3, 4] = 0.5
fig = fig = plt.figure()
s_plot = StackPlot(x, y_data,
axes=fig.add_subplot(111),
names=['a', 'b', 'c', 'd', 'e'])
s_plot.set_title('My Stacked Plot')
s_plot.set_xlabel('x')
s_plot.set_ylabel('y')
s_plot.draw()
plt.show()
#20
#21
#22
#23
#24
#25
#26
test()
Finally, we test our program (#20). We create data for x (#22) and y (#23). We replace some data in y, that is
only ones, with some different numbers. Now we can add our new stacked plot to our axes (#23). This
argument is optional. Leaving it out, the class will use the current axes. We set some properties (#24), create
the plot (#25) and show our figure (#26).
11.1 Exercises
1. Change some y data in the test function and see how this changes the graph. Increase the number of
stacked plots to 50 and 200. See what happens.
2. Place four stacked plots in one figure. Change the data slightly for each stacked plot.
3. Move the legend to different place for each of the four plots.
12 Animations
12 Animations
Animations can be useful to show how things change over time. There are two basic ways to produce an
animation with matplotlib:
1. Saving pixel graphics for each animation frame and stitch them together with tools like imagemagick or
ffmpeg.
2. Having an animation directly in the figure.
The first approach is straight forward. The second typically needs some interaction with the GUI library. Let's
have a look at an example:
"""Animation with matplolib.
"""
import random
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
#1
#2
After importing matplotlib (#1), we need to tell what GUI to use. We use TKinter here (#2). Other
supported tool kits are Qt, wxPython and GTK. The solution presented here is Tkinter specific.
class Residual(object):
"""Generator for residual values.
#3
#4
def __call__(self):
if self.done:
return None
diff = random.random() * self.value
if self.counter == 2:
self.value += diff
self.counter = 0
else:
self.value -= diff
self.counter += 1
if self.value <= self.limit:
self.done = True
return self.value
#5
#6
#7
#8
#9
#10
12 Animations
The class Residual (#3) will be used as function that keeps its state. It is a placeholder for a numerical
program that solves something interactively and is finished if a certain value of the residual is reached. The
__int__ (#4) takes the starting value and the limit to be reached. We implement the special method
__call__ (#5) making an instance of this class behave just like a function but with the additional feature of
keeping state information in self. If we are done, the limit was reached, we return None (#6). The difference
leading to a new residual is determined by multiplying the old value with a random number between 0 and 1
(#7). We increment the residual by diff once (#8) after decrementing it twice (#9). If the residual is less or
equal to the limit, we are done (#10).
class ResidualGraph(object):
"""Semilog plot with matplotlib.
#11
#12
#13
#14
#15
#16
#17
#18
The ResidualGraph (#11) is responsible for displaying the animated figure. After creating a new figure
(#12) and adding one subplot (#13), we start with a counter of zero (#14) and use it as the first and so far
only value for x (#15). The value makes the first entry in our y values (#16). After initializing the graph, we
need to get a hold of the window (#18). This will be needed to start the animation as wee will see later.
def show_initial_graph(self):
"""Show a first version of the graph without calculated residuals.
"""
self.axes.semilogy(self.x, self.y)
#19
self.axes.set_title('Solver test program')
self.axes.set_xlabel('Number of steps')
self.axes.set_ylabel('Nonlinear residual')
self.fig.canvas.draw()
#20
Initializing the graph is straight forward. We use a semi-log plot (#19). After adding title and labels, we draw
our plot (#20).
def update(self, value):
#21
"""Redraw the graph with an added value for the residual.
"""
self.counter += 1
#22
self.x.append(self.counter)
#23
self.y.append(value)
#24
12.1 Exercises
#25
#26
The update-method (#21) is important, because it will redraw the graph for each animation step. After
incrementing the counter (#22) and adding it to the x values (#23), we append the new residual value to
our y values (#24) and make new plot (#25) that we draw (#26).
def start_animation(start, limit):
"""Start the animation.
"""
#27
def animate():
#28
"""Animation function will be called by GUI library (Tkinter).
"""
residual = get_residual()
#29
# show value and update graph
if residual is not None:
#30
graph.window.after(300, animate)
#31
print residual
graph.update(residual)
#32
else:
print 'done'
#33
get_residual = Residual(start, limit)
#34
graph = ResidualGraph(start)
#35
graph.window.after(300, animate)
#36
plt.show()
#37
In start_animation (#27), we define a nested function animate (#28). This function will be called by
Tkinter after a specified amount of time. We get the new value for the residual from get_residual(), which
is an instance of the class Residual (#34). As long as we have a value for the residual (#30), we call
window.after with a delay of 300 milliseconds and the function animate, i.e. the function we just define
(#31). The object window is from Tkinter (#18). We also update the graph (#32) which is an instance of
ResidualGraph (#35). The first call to window.after (#36) starts the animation. We also want the plot
stay alive after the animation is finished and therefore call show (#37).
if __name__ == '__main__':
start_animation(1e4, 1e-7)
#38
Finally, we start everything with some numbers spanning eleven orders of magnitude (#38).
12.1 Exercises
1. Alternate the color of the plotted line with each animation step.