Note There is now an accompanying numpy tutorial. Matplotlib tutorial Nicolas P. Rougier - Euroscipy 2012 - Prace 2013 - Euroscipy 2013 Introduction Simple plot Figures, Subplots, Axes and Ticks Other Types of Plots Beyond this tutorial Quick references This tutorial is based on Mike Müller's tutorial available from the scipy lecture notes. Sources are available here. Figures are in the figures directory and all scripts are located in the scripts directory. Github repository is here All code and material is licensed under a Creative Commons Attribution 3.0 United States License (CC-by) http://creativecommons.org/licenses/by/3.0/us Many thanks to Bill Wing and Christoph Deil for review and corrections. Introductory slides on scientific visualization are here 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.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Note
There is now an accompanying
numpy tutorial.
Matplotlib tutorialNicolas P. Rougier - Euroscipy 2012 - Prace 2013 -
Euroscipy 2013
Introduction
Simple plot
Figures, Subplots, Axes and Ticks
Other Types of Plots
Beyond this tutorial
Quick references
This tutorial is based on Mike Müller's tutorial available from the scipy
lecture notes.
Sources are available here. Figures are in the figures directory and all
scripts are located in the scripts directory. Github repository is here
All code and material is licensed under a Creative Commons
Attribution 3.0 United States License (CC-by)
http://creativecommons.org/licenses/by/3.0/us
Many thanks to Bill Wing and Christoph Deil for review and
corrections.
Introductory slides on scientific visualization are here
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
The code is fairly well documented and you can quickly access a
specific command from within a python session:
>>> from pylab import *>>> help(plot)Help on function plot in module matplotlib.pyplot:
plot(*args, **kwargs) Plot lines and/or markers to the :class:`~matplotlib.axes.Axes`. *args* is a variable length argument, allowing for multiple *x*, *y* pairs with an optional format string. For example, each of the following is legal::
plot(x, y) # plot x and y using default line style and color plot(x, y, 'bo') # plot x and y using blue circle markers plot(y) # plot y using x as index array 0..N-1 plot(y, 'r+') # ditto, but with red plusses
If *x* and/or *y* is 2-dimensional, then the corresponding columns will be plotted. ...
Galleries
The matplotlib gallery is also incredibly useful when you search how
to render a given graphic. Each example comes with its source.
A smaller gallery is also available here.
Mailing lists
Finally, there is a user mailing list where you can ask for help and a
Basic Beginners’ Introduction to plotting in Python
Sarah Blyth
July 23, 2009
1 Introduction
Welcome to a very short introduction on getting started with plotting in Python!I would highly recommend that you refer to the official Matplotlib documentationwhich can be found at:http://matplotlib.sourceforge.net/contents.html.You can also download the Matplotlib manual from the Astronomy Department Vularepository (faster) or directly from the Matplotlib website (slower).
There are all sorts of examples and further very important and useful detailslisted and explained there. The aim of this document is merely to start you off onDay 1 - how to make a plot from scratch.
2 Getting started - what do you need?
You need to have the following installed on your computer to be able to make niceplots.
• Python
• Numpy - this is the module which does most array and mathematical manip-ulation
• Matplotlib - this is the module you will be using for plotting
You can check these are installed by going to a terminal and typing:
$ python>>> import numpy as np>>> import pylab as pl
If you get no errors, then they are installed. If they are not installed, speak to thedepartment SysAdmin to help you install them.You can also check which versions you have installed by doing:
Two basic plot types which you will find are used very often are (x,y) line and scatterplots and histograms. Some code for making these two types of plots is included inthis section.
3.1 Line and scatter plots
3.1.1 Line plots
A common type of graph to plot is a line relating x-values to particular y-values.The code to draw the graph in Fig. 1 is below:
# Make an array of x valuesx = [1, 2, 3, 4, 5]# Make an array of y values for each x valuey = [1, 4, 9, 16, 25]
# use pylab to plot x and ypl.plot(x, y)
# show the plot on the screenpl.show()****************************************************
2
Figure 1: Line plot made with lineplot.py
3.1.2 Scatter plots
Alternatively, you may want to plot quantities which have an x and y position. Forexample, plotting the location of stars or galaxies in a field for example such as theplot in Fig. 2. Try running the script below to do this:
# Make an array of x valuesx = [1, 2, 3, 4, 5]# Make an array of y values for each x valuey = [1, 4, 9, 16, 25]
# use pylab to plot x and y as red circlespl.plot(x, y, ’ro’)
# show the plot on the screenpl.show()****************************************************
3
Figure 2: Plot made with scatterplot.py
3.2 Making things look pretty
3.2.1 Changing the line color
It is very useful to be able to plot more than one set of data on the same axes and tobe able to differentiate between them by using different line and marker styles andcolours. You can specify the colour by inserting a 3rd parameter into the plot()command. For example, in lineplot.py, try changing the line
pl.plot(x, y)
to
pl.plot(x, y, ’r’)
This should give you the same line as before, but it should now be red.The other colours you can easily use are:
character colorb blueg greenr redc cyanm magentay yellowk blackw white
4
3.2.2 Changing the line style
You can also change the style of the line e.g. to be dotted, dashed, etc. Try:
plot(x,y, ’--’)
This should give you a dashed line now.Other linestyles you can use can be found on the Matplotlib webpage http://matplotlib.sourceforge.net/api/pyplot api.html#matplotlib.pyplot.plot.
3.2.3 Changing the marker style
Lastly, you can also vary the marker style you use. Try:
plot(x,y, ’b*’)
This should give you blue star-shaped markers. The table below gives some moreoptions for setting marker types:
’s’ square marker’p’ pentagon marker’*’ star marker’h’ hexagon1 marker’H’ hexagon2 marker’+’ plus marker’x’ x marker’D’ diamond marker’d’ thin diamond marker
3.2.4 Plot and axis titles and limits
It is very important to always label the axes of plots to tell the viewer what they arelooking at. You can do this in python by using the commands:
pl.xlabel(’put text here’)pl.ylabel(’put text here’)
You can make a title for your plot by:
pl.title(’Put plot title here’)
You can change the x and y ranges displayed on your plot by:
pl.xlim(x_low, x_high)pl.ylim(y_low, y_high)
Have a look at the modified macro lineplotAxis.py below:
# Make x, y arrays for each graphx1 = [1, 2, 3, 4, 5]y1 = [1, 4, 9, 16, 25]
x2 = [1, 2, 4, 6, 8]y2 = [2, 4, 8, 12, 16]
# use pylab to plot x and ypl.plot(x1, y1, ’r’)pl.plot(x2, y2, ’g’)
# give plot a titlepl.title(’Plot of y vs. x’)# make axis labelspl.xlabel(’x axis’)pl.ylabel(’y axis’)
# set axis limitspl.xlim(0.0, 9.0)pl.ylim(0.0, 30.)
# show the plot on the screenpl.show()****************************************************
7
Figure 4: Plot made with lineplot2Plots.py
3.2.6 Figure legends
It’s very useful to add legends to plots to differentiate between the different lines orquantities being plotted. In python you can make a legend as follows:
The first parameter is a list of the plots you want labelled. The second parameter isthe list of labels. The third parameter is where you would like matplotlib to placeyour legend. Other optiions are:‘upper right’, ‘upper left’, ‘center’, ‘lower left’, ‘lower right’.‘best’ means that matplotlib will try to choose the position on your plot where thelegend will interfere least with what is plotted (i.e. avoid overlaps etc.).Have a look at Fig. 5 which is made using the macro below:
# Make x, y arrays for each graphx1 = [1, 2, 3, 4, 5]y1 = [1, 4, 9, 16, 25]
x2 = [1, 2, 4, 6, 8]y2 = [2, 4, 8, 12, 16]
8
# use pylab to plot x and y : Give your plots namesplot1 = pl.plot(x1, y1, ’r’)plot2 = pl.plot(x2, y2, ’go’)
# give plot a titlepl.title(’Plot of y vs. x’)# make axis labelspl.xlabel(’x axis’)pl.ylabel(’y axis’)
# set axis limitspl.xlim(0.0, 9.0)pl.ylim(0.0, 30.)
# make legendpl.legend([plot1, plot2], (’red line’, ’green circles’), ’best’, numpoints=1)
# show the plot on the screenpl.show()****************************************************
Figure 5: Plot made with lineplotFigLegend.py
3.3 Histograms
Histograms are very often used in science applications and it is highly likely thatyou will need to plot them at some point! They are very useful to plot distributions
9
e.g. what is the distribution of galaxy velocities in my sample? etc. In Matplotlibyou use the hist command to make a histogram. Take a look at the short macrobelow which makes the plot shown in Fig. 6:
# make an array of random numbers with a gaussian distribution with# mean = 5.0# rms = 3.0# number of points = 1000data = np.random.normal(5.0, 3.0, 1000)
If you don’t want to see the black outlines between the bars in the histogram,try:
pl.hist(data, histtype=’stepfilled’)
This is how you make the plot in Fig. 7.
Figure 6: Plot made withhistplot.py
Figure 7: Plot made withhistplot.py
10
3.3.1 Setting the width of the histogram bins manually
You can also set the width of your histogram bins yourself. Try adding the followinglines to the macro histplot.py and you should get the plot shown in Fig. 8.
Figure 8: Manually setting the bin width for a histogram
4 Plotting more than one plot per canvas
Matplotlib is reasonably flexible about allowing multiple plots per canvas and it iseasy to set this up. You need to first make a figure and then specify subplots asfollows:
fig1 = pl.figure(1)pl.subplot(211)
subplot(211) means that you will have a figure with 2 rows, 1 column, and you’regoing to draw in the top plot as shown in Fig. 9. If you want to plot something inthe lower section (as shown in Fig. 10), then you do:
pl.subplot(212)
You can play around with plotting a variety of layouts. For example, Fig. 11 iscreated using the following commands:
Often you will have data in ascii (text) format which you will need to plot in someway. In this section we will briefly discuss how to open, read from, and write to,files.
5.1 Reading data from ascii files
There are many ways to read data from a file in python. Here I am going to illus-trate one simple method, but you should read the Python, Numpy and Matplotlibmanuals to find out about the other ways which might sometimes apply better toyou particular situation.You can use Numpy to read in numerical values from a text file. For example, let’stake the text file called fakedata.txt which contains the following data (2 columnsof numbers):
There are also various ways to write text to a text file. Here we show you one possibleway to do it.You first have to open a file to write to, then write what you want to the file, andthen, do not forget to close the file! See the macro below, writeToFile.py to seehow to do this:
# Let’s make 2 arrays (x, y) which we will write to a file
# x is an array containing numbers 0 to 10, with intervals of 1x = np.arange(0.0, 10., 1.)# y is an array containing the values in x, squaredy = x*x
print ’x = ’, xprint ’y = ’, y
# Now open a file to write the data to# ’w’ means open for ’writing’file = open(’testdata.txt’, ’w’)
# loop over each line you want to write to filefor i in range(len(x)):
# make a string for each line you want to write# ’\t’ means ’tab’# ’\n’ means ’newline’# ’str()’ means you are converting the quantity in brackets to a string typetxt = str(x[i]) + ’\t’ + str(y[i]) + ’ \n’# write the txt to the filefile.write(txt)
# Close your filefile.close()****************************************************
You should then check that the file you created looks as you would expect! Aquick way to do this on the Linux/Unix terminal command line is:
$ more testdata.txt
This command will print out the contents of your file to the screen and you willquickly be able to check if things look right!
15
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
MatplotlibA tutorial
Devert AlexandreSchool of Software Engineering of USTC
30 November 2012 — Slide 1/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Table of Contents
1 First steps
2 Curve plots
3 Scatter plots
4 Boxplots
5 Histograms
6 Usage example
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 2/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Curve plot
Let’s plot a curve
impor t mathimpor t ma t p l o t l i b . p yp l o t as p l t
# Genera te a s i n u s o i dnbSamples = 256xRange = (−math . p i , math . p i )
x , y = [ ] , [ ]f o r n i n x range ( nbSamples ) :
k = (n + 0 . 5 ) / nbSamplesx . append ( xRange [ 0 ] + ( xRange [ 1 ] − xRange [ 0 ] ) ∗ k )y . append (math . s i n ( x [−1]))
# P lo t the s i n u s o i dp l t . p l o t ( x , y )p l t . show ( )
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 3/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Curve plot
This will show you something like this
4 3 2 1 0 1 2 3 41.0
0.5
0.0
0.5
1.0
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 4/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
numpy
matplotlib can work with numpy arrays
impor t mathimpor t numpyimpor t ma t p l o t l i b . p yp l o t as p l t
# Genera te a s i n u s o i dnbSamples = 256xRange = (−math . p i , math . p i )
x , y = numpy . z e r o s ( nbSamples ) , numpy . z e r o s ( nbSamples )f o r n i n x range ( nbSamples ) :
k = (n + 0 . 5 ) / nbSamplesx [ n ] = xRange [ 0 ] + ( xRange [ 1 ] − xRange [ 0 ] ) ∗ ky [ n ] = math . s i n ( x [ n ] )
# P lo t the s i n u s o i dp l t . p l o t ( x , y )p l t . show ( )
numpy provides a lot of function and is efficient
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 5/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
numpy
• zeros build arrays filled of 0
• linspace build arrays filled with an arithmeticsequence
impor t mathimpor t numpyimpor t ma t p l o t l i b . p yp l o t as p l t
# Genera te a s i n u s o i dx = numpy . l i n s p a c e (−math . p i , math . p i , num=256)y = numpy . z e r o s ( nbSamples )f o r n i n x range ( nbSamples ) :
y [ n ] = math . s i n ( x [ n ] )
# P lo t the s i n u s o i dp l t . p l o t ( x , y )p l t . show ( )
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 6/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
numpy
numpy functions can work on entire arrays
impor t mathimpor t numpyimpor t ma t p l o t l i b . p yp l o t as p l t
# Genera te a s i n u s o i dx = numpy . l i n s p a c e (−math . p i , math . p i , num=256)y = numpy . s i n ( x )
# P lo t the s i n u s o i dp l t . p l o t ( x , y )p l t . show ( )
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 7/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
PDF output
Exporting to a PDF file is just one change
impor t mathimpor t numpyimpor t ma t p l o t l i b . p yp l o t as p l t
# Genera te a s i n u s o i dx = numpy . l i n s p a c e (−math . p i , math . p i , num=256)y = numpy . s i n ( x )
# P lo t the s i n u s o i dp l t . p l o t ( x , y )p l t . s a v e f i g ( ’ s i n−p l o t . pdf ’ , t r a n s p a r e n t=True )
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 8/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Table of Contents
1 First steps
2 Curve plots
3 Scatter plots
4 Boxplots
5 Histograms
6 Usage example
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 9/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Multiple curves
It’s often convenient to show several curves in one figure
impor t mathimpor t numpyimpor t ma t p l o t l i b . p yp l o t as p l t
# Genera te a s i n u s o i d and a c o s i n o i dx = numpy . l i n s p a c e (−math . p i , math . p i , num=256)y = numpy . s i n ( x )z = numpy . cos ( x )
# P lo t the cu r v e sp l t . p l o t ( x , y )p l t . p l o t ( x , z )p l t . show ( )
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 10/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Multiple curves
It’s often convenient to show several curves in one figure
4 3 2 1 0 1 2 3 41.0
0.5
0.0
0.5
1.0
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 10/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Custom colors
Changing colors can help to make nice documents
impor t mathimpor t numpyimpor t ma t p l o t l i b . p yp l o t as p l t
# Genera te a s i n u s o i d and a c o s i n o i dx = numpy . l i n s p a c e (−math . p i , math . p i , num=256)y = numpy . s i n ( x )z = numpy . cos ( x )
# P lo t the cu r v e sp l t . p l o t ( x , y , c=’#FF4500 ’ )p l t . p l o t ( x , z , c=’#4682B4 ’ )p l t . show ( )
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 11/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Custom colors
Changing colors can help to make nice documents
4 3 2 1 0 1 2 3 41.0
0.5
0.0
0.5
1.0
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 11/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Line thickness
Line thickness can be changed as well
impor t mathimpor t numpyimpor t ma t p l o t l i b . p yp l o t as p l t
# Genera te a s i n u s o i d and a c o s i n o i dx = numpy . l i n s p a c e (−math . p i , math . p i , num=256)y = numpy . s i n ( x )z = numpy . cos ( x )
# P lo t the cu r v e sp l t . p l o t ( x , y , l i n ew i d t h =3, c=’#FF4500 ’ )p l t . p l o t ( x , z , c=’#4682B4 ’ )p l t . show ( )
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 12/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Line thickness
Line thickness can be changed as well
4 3 2 1 0 1 2 3 41.0
0.5
0.0
0.5
1.0
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 12/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Line patterns
For printed document, colors can be replaced by linepatterns
impor t mathimpor t numpyimpor t ma t p l o t l i b . p yp l o t as p l t
# L i n e s t y l e s can be ’− ’ , ’−−’, ’− . ’ , ’ : ’
# Genera te a s i n u s o i d and a c o s i n o i dx = numpy . l i n s p a c e (−math . p i , math . p i , num=256)y = numpy . s i n ( x )z = numpy . cos ( x )
# P lo t the cu r v e sp l t . p l o t ( x , y , l i n e s t y l e=’−− ’ , c=’#000000 ’ )p l t . p l o t ( x , z , c=’#808080 ’ )p l t . show ( )
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 13/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Line patternsFor printed document, colors can be replaced by linepatterns
4 3 2 1 0 1 2 3 41.0
0.5
0.0
0.5
1.0
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 13/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Markers
It sometime relevant to show the data points
impor t mathimpor t numpyimpor t ma t p l o t l i b . p yp l o t as p l t
# Markers can be ’ . ’ , ’ , ’ , ’ o ’ , ’ 1 ’ and more
# Genera te a s i n u s o i d and a c o s i n o i dx = numpy . l i n s p a c e (−math . p i , math . p i , num=64)y = numpy . s i n ( x )z = numpy . cos ( x )
# P lo t the cu r v e sp l t . p l o t ( x , y , marker=’ 1 ’ , ma r k e r s i z e =15, c=’#000000 ’ )p l t . p l o t ( x , z , c=’#000000 ’ )p l t . show ( )
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 14/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Markers
It sometime relevant to show the data points
4 3 2 1 0 1 2 3 41.0
0.5
0.0
0.5
1.0
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 14/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Legend
A legend can help to make self–explanatory figures
impor t mathimpor t numpyimpor t ma t p l o t l i b . p yp l o t as p l t
# l egend l o c a t i o n can be ’ b e s t ’ , ’ c e n t e r ’ , ’ l e f t ’ , ’ r i g h t ’ , e t c .
# Genera te a s i n u s o i d and a c o s i n o i dx = numpy . l i n s p a c e (−math . p i , math . p i , num=256)y = numpy . s i n ( x )z = numpy . cos ( x )
# P lo t the cu r v e sp l t . p l o t ( x , y , c=’#FF4500 ’ , l a b e l= ’ s i n ( x ) ’ )p l t . p l o t ( x , z , c=’#4682B4 ’ , l a b e l=’ cos ( x ) ’ )p l t . l e g end ( l o c=’ b e s t ’ )p l t . show ( )
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 15/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Legend
A legend can help to make self–explanatory figures
4 3 2 1 0 1 2 3 41.0
0.5
0.0
0.5
1.0
sin(x)cos(x)
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 15/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Custom axis scaleChanging the axis scale can improve readability
impor t mathimpor t numpyimpor t ma t p l o t l i b . p yp l o t as p l t
# l egend l o c a t i o n can be ’ b e s t ’ , ’ c e n t e r ’ , ’ l e f t ’ , ’ r i g h t ’ , e t c .
# Genera te a s i n u s o i d and a c o s i n o i dx = numpy . l i n s p a c e (−math . p i , math . p i , num=256)y = numpy . s i n ( x )z = numpy . cos ( x )
# Ax i s s e tupf i g = p l t . f i g u r e ( )a x i s = f i g . a dd subp l o t (111)
a x i s . s e t y l i m (−0.5 ∗ math . p i , 0 . 5 ∗ math . p i )
# P lo t the cu r v e sp l t . p l o t ( x , y , c=’#FF4500 ’ , l a b e l= ’ s i n ( x ) ’ )p l t . p l o t ( x , z , c=’#4682B4 ’ , l a b e l=’ cos ( x ) ’ )p l t . l e g end ( l o c=’ b e s t ’ )p l t . show ( )
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 16/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Custom axis scale
Changing the axis scale can improve readability
4 3 2 1 0 1 2 3 41.5
1.0
0.5
0.0
0.5
1.0
1.5sin(x)cos(x)
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 16/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
GridSame goes for a grid, can be helpful
impor t mathimpor t numpyimpor t ma t p l o t l i b . p yp l o t as p l t
# l egend l o c a t i o n can be ’ b e s t ’ , ’ c e n t e r ’ , ’ l e f t ’ , ’ r i g h t ’ , e t c .
# Genera te a s i n u s o i d and a c o s i n o i dx = numpy . l i n s p a c e (−math . p i , math . p i , num=256)y = numpy . s i n ( x )z = numpy . cos ( x )
# Ax i s s e tupf i g = p l t . f i g u r e ( )a x i s = f i g . a dd subp l o t (111)
a x i s . s e t y l i m (−0.5 ∗ math . p i , 0 . 5 ∗ math . p i )a x i s . g r i d ( True )
# P lo t the cu r v e sp l t . p l o t ( x , y , c=’#FF4500 ’ , l a b e l= ’ s i n ( x ) ’ )p l t . p l o t ( x , z , c=’#4682B4 ’ , l a b e l=’ cos ( x ) ’ )p l t . l e g end ( l o c=’ b e s t ’ )p l t . show ( )
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 17/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Grid
Same goes for a grid, can be helpful
4 3 2 1 0 1 2 3 41.5
1.0
0.5
0.0
0.5
1.0
1.5sin(x)cos(x)
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 17/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Error bars
Your data might come with a known measure error
impor t mathimpor t numpyimpor t numpy . randomimpor t ma t p l o t l i b . p yp l o t as p l t
# Genera te a n o i s y s i n u s o i dx = numpy . l i n s p a c e (−math . p i , math . p i , num=48)y = numpy . s i n ( x + 0.05 ∗ numpy . random . s t anda rd no rma l ( l e n ( x ) ) )y e r r o r = 0 .1 ∗ numpy . random . s t anda rd no rma l ( l e n ( x ) )
# Ax i s s e tupf i g = p l t . f i g u r e ( )a x i s = f i g . a dd subp l o t (111)
a x i s . s e t y l i m (−0.5 ∗ math . p i , 0 . 5 ∗ math . p i )
# P lo t the cu r v e sp l t . p l o t ( x , y , c=’#FF4500 ’ )p l t . e r r o r b a r ( x , y , y e r r=y e r r o r )p l t . show ( )
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 18/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Error bars
Your data might come with a known measure error
4 3 2 1 0 1 2 3 41.5
1.0
0.5
0.0
0.5
1.0
1.5
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 18/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Table of Contents
1 First steps
2 Curve plots
3 Scatter plots
4 Boxplots
5 Histograms
6 Usage example
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 19/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Scatter plot
A scatter plot just shows one point for each dataset entry
impor t numpyimpor t numpy . randomimpor t ma t p l o t l i b . p yp l o t as p l t
# Genera te a 2d normal d i s t r i b u t i o nnbPo in t s = 100x = numpy . random . s t anda rd no rma l ( nbPo in t s )y = numpy . random . s t anda rd no rma l ( nbPo in t s )
# P lo t the p o i n t sp l t . s c a t t e r ( x , y )p l t . show ( )
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 20/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Scatter plotA scatter plot just shows one point for each dataset entry
3 2 1 0 1 2 34
3
2
1
0
1
2
3
4
5
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 20/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Aspect ratio
If can be very important to have the same scale on bothaxis
impor t numpyimpor t numpy . randomimpor t ma t p l o t l i b . p yp l o t as p l t
# Genera te a 2d normal d i s t r i b u t i o nnbPo in t s = 100x = numpy . random . s t anda rd no rma l ( nbPo in t s )y = 0 .1 ∗ numpy . random . s t anda rd no rma l ( nbPo in t s )
# Ax i s s e tupf i g = p l t . f i g u r e ( )a x i s = f i g . a dd subp l o t (111 , a sp e c t=’ equa l ’ )
# P lo t the p o i n t sp l t . s c a t t e r ( x , y , c=’#FF4500 ’ )p l t . show ( )
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 21/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Aspect ratioIf can be very important to have the same scale on bothaxis
3 2 1 0 1 2 30.30.20.10.00.10.20.3
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 21/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Aspect ratioAlternative way to keep the same scale on both axis
impor t numpyimpor t numpy . randomimpor t ma t p l o t l i b . p yp l o t as p l t
# Genera te a 2d normal d i s t r i b u t i o nnbPo in t s = 100x = numpy . random . s t anda rd no rma l ( nbPo in t s )y = 0 .1 ∗ numpy . random . s t anda rd no rma l ( nbPo in t s )
# Ax i s s e tupf i g = p l t . f i g u r e ( )a x i s = f i g . a dd subp l o t (111)
cmin , cmax = min (min ( x ) , min ( y ) ) , max(max( x ) , max( y ) )cmin −= 0.05 ∗ ( cmax − cmin )cmax += 0.05 ∗ ( cmax − cmin )
a x i s . s e t x l i m ( cmin , cmax )a x i s . s e t y l i m ( cmin , cmax )
# P lo t the p o i n t sp l t . s c a t t e r ( x , y , c=’#FF4500 ’ )p l t . show ( )
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 22/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Aspect ratio
Alternative way to keep the same scale on both axis
2 1 0 1 2
2
1
0
1
2
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 22/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Multiple scatter plotsAs for curve, you can show 2 datasets on one figure
impor t numpyimpor t numpy . randomimpor t ma t p l o t l i b . p yp l o t as p l t
c o l o r s = ( ’#FF4500 ’ , ’#3CB371 ’ , ’#4682B4 ’ , ’#DB7093 ’ , ’#FFD700 ’ )
# Genera te a 2d normal d i s t r i b u t i o nnbPo in t s = 100
x , y = [ ] , [ ]
x += [ numpy . random . s t anda rd no rma l ( nbPo in t s ) ]y += [ 0 . 2 5 ∗ numpy . random . s t anda rd no rma l ( nbPo in t s ) ]
x += [ 0 . 5 ∗ numpy . random . s t anda rd no rma l ( nbPo in t s ) + 3 . 0 ]y += [2 ∗ numpy . random . s t anda rd no rma l ( nbPo in t s ) + 2 . 0 ]
# Ax i s s e tupf i g = p l t . f i g u r e ( )a x i s = f i g . a dd subp l o t (111 , a sp e c t=’ equa l ’ )
# P lo t the p o i n t sf o r i i n x range ( l e n ( x ) ) :
p l t . s c a t t e r ( x [ i ] , y [ i ] , c=c o l o r s [ i % l e n ( c o l o r s ) ] )
p l t . show ( )
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 23/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Multiple scatter plots
As for curve, you can show 2 datasets on one figure
3 2 1 0 1 2 3 4 54
2
0
2
4
6
8
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 23/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Showing centersIt can help to see the centers or the median points
impor t numpyimpor t numpy . randomimpor t ma t p l o t l i b . p yp l o t as p l t
c o l o r s = ( ’#FF4500 ’ , ’#3CB371 ’ , ’#4682B4 ’ , ’#DB7093 ’ , ’#FFD700 ’ )
# Genera te a 2d normal d i s t r i b u t i o nnbPo in t s = 100
x , y = [ ] , [ ]
x += [ numpy . random . s t anda rd no rma l ( nbPo in t s ) ]y += [ 0 . 2 5 ∗ numpy . random . s t anda rd no rma l ( nbPo in t s ) ]
x += [ 0 . 5 ∗ numpy . random . s t anda rd no rma l ( nbPo in t s ) + 3 . 0 ]y += [2 ∗ numpy . random . s t anda rd no rma l ( nbPo in t s ) + 2 . 0 ]
# Ax i s s e tupf i g = p l t . f i g u r e ( )a x i s = f i g . a dd subp l o t (111 , a sp e c t=’ equa l ’ )
# P lo t the p o i n t sf o r i i n x range ( l e n ( x ) ) :
c o l = c o l o r s [ i % l e n ( c o l o r s ) ]p l t . s c a t t e r ( x [ i ] , y [ i ] , c=c o l )p l t . s c a t t e r ( [ numpy . median ( x [ i ] ) ] , [ numpy . median ( y [ i ] ) ] , c=co l , s=250)
p l t . show ( )
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 24/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Showing centers
It can help to see the centers or the median points
3 2 1 0 1 2 3 4 54
2
0
2
4
6
8
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 24/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Marker stylesYou can use different markers styles
impor t numpyimpor t numpy . randomimpor t ma t p l o t l i b . p yp l o t as p l t
markers = ( ’+’ , ’ ˆ ’ , ’ . ’ )
# Genera te a 2d normal d i s t r i b u t i o nnbPo in t s = 100
x , y = [ ] , [ ]
x += [ numpy . random . s t anda rd no rma l ( nbPo in t s ) ]y += [ 0 . 2 5 ∗ numpy . random . s t anda rd no rma l ( nbPo in t s ) ]
x += [ 0 . 5 ∗ numpy . random . s t anda rd no rma l ( nbPo in t s ) + 3 . 0 ]y += [2 ∗ numpy . random . s t anda rd no rma l ( nbPo in t s ) + 2 . 0 ]
# Ax i s s e tupf i g = p l t . f i g u r e ( )a x i s = f i g . a dd subp l o t (111 , a sp e c t=’ equa l ’ )
# P lo t the p o i n t sf o r i i n x range ( l e n ( x ) ) :m = markers [ i % l e n ( markers ) ]p l t . s c a t t e r ( x [ i ] , y [ i ] , marker=m, c=’#000000 ’ )p l t . s c a t t e r ( [ numpy . median ( x [ i ] ) ] , [ numpy . median ( y [ i ] ) ] , marker=m, s=250 , c=’#000000 ’ )
p l t . show ( )
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 25/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Marker styles
You can use different markers styles
4 3 2 1 0 1 2 3 4 54
2
0
2
4
6
8
10
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 25/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Table of Contents
1 First steps
2 Curve plots
3 Scatter plots
4 Boxplots
5 Histograms
6 Usage example
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 26/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Boxplots
Let’s do a simple boxplot
impor t numpyimpor t numpy . randomimpor t ma t p l o t l i b . p yp l o t as p l t
# Genera te normal d i s t r i b u t i o n datax = numpy . random . s t anda rd no rma l (256)
# Show a boxp l o t o f the datap l t . b o xp l o t ( x )p l t . show ( )
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 27/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Boxplots
Let’s do a simple boxplot
13
2
1
0
1
2
3
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 27/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Boxplots
You might want to show the original data in the sametime
impor t numpyimpor t numpy . randomimpor t ma t p l o t l i b . p yp l o t as p l t
# Genera te normal d i s t r i b u t i o n datax = numpy . random . s t anda rd no rma l (256)
# Show a boxp l o t o f the datap l t . s c a t t e r ( [ 0 ] ∗ l e n ( x ) , x , c=’#4682B4 ’ )p l t . b o xp l o t ( x , p o s i t i o n s =[0 ] )p l t . show ( )
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 28/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
BoxplotsYou might want to show the original data in the sametime
04
3
2
1
0
1
2
3
4
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 28/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Multiple boxplots
Boxplots are often used to show side by side variousdistributions
impor t numpyimpor t numpy . randomimpor t ma t p l o t l i b . p yp l o t as p l t
# Genera te normal d i s t r i b u t i o n datadata = [ ]f o r i i n x range ( 5 ) :
mu = 10 ∗ numpy . random . random sample ( )s igma = 2 ∗ numpy . random . random sample ( ) + 0 .1data . append (numpy . random . normal (mu, sigma , 256))
# Show a boxp l o t o f the datap l t . b o xp l o t ( data )p l t . show ( )
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 29/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Multiple boxplotsBoxplots are often used to show side by side variousdistributions
1 2 3 4 52
4
6
8
10
12
14
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 29/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Orientation
Changing the orientation is easily done
impor t numpyimpor t numpy . randomimpor t ma t p l o t l i b . p yp l o t as p l t
# Genera te normal d i s t r i b u t i o n datadata = [ ]f o r i i n x range ( 5 ) :
mu = 10 ∗ numpy . random . random sample ( )s igma = 2 ∗ numpy . random . random sample ( ) + 0 .1data . append (numpy . random . normal (mu, sigma , 256))
# Show a boxp l o t o f the datap l t . b o xp l o t ( data , v e r t =0)p l t . show ( )
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 30/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Orientation
Changing the orientation is easily done
6 4 2 0 2 4 6 8 10 12
1
2
3
4
5
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 30/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
LegendGood graphics have a proper legend
impor t numpyimpor t numpy . randomimpor t ma t p l o t l i b . p yp l o t as p l t
# Genera te normal d i s t r i b u t i o n datal a b e l s = [ ’ mercury ’ , ’ l e a d ’ , ’ l i t h i um ’ , ’ t ung s t ene ’ , ’ cadnium ’ ]data = [ ]f o r i i n x range ( l e n ( l a b e l s ) ) :
mu = 10 ∗ numpy . random . random sample ( ) + 100sigma = 2 ∗ numpy . random . random sample ( ) + 0 .1data . append (numpy . random . normal (mu, sigma , 256))
# Ax i s s e tupf i g = p l t . f i g u r e ( )a x i s = f i g . a dd subp l o t (111)
a x i s . s e t t i t l e ( ’ A l i e n nodu l e s compos i t i on ’ )xt ickNames = p l t . s e t p ( a x i s , x t i c k l a b e l s = l a b e l s )a x i s . s e t y l a b e l ( ’ c o n c e n t r a t i o n (ppm) ’ )
# Show a boxp l o t o f the datap l t . b o xp l o t ( data )p l t . show ( )
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 31/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Legend
Good graphics have a proper legend
mercury lead lithium tungstene cadnium100
102
104
106
108
110
112co
nce
ntr
ati
on (
ppm
)Alien nodules composition
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 31/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Table of Contents
1 First steps
2 Curve plots
3 Scatter plots
4 Boxplots
5 Histograms
6 Usage example
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 32/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Histograms
Histogram are convenient to sum-up results
impor t numpyimpor t numpy . randomimpor t ma t p l o t l i b . p yp l o t as p l t
# Some datadata = numpy . abs (numpy . random . s t anda rd no rma l ( 30 ) )
# Show an h i s tog ramp l t . barh ( range ( l e n ( data ) ) , data , c o l o r=’#4682B4 ’ )p l t . show ( )
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 33/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Histograms
Histogram are convenient to sum-up results
0.0 0.5 1.0 1.5 2.0 2.50
5
10
15
20
25
30
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 33/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Histograms
A variant to show 2 quantities per item on 1 figure
impor t numpyimpor t numpy . randomimpor t ma t p l o t l i b . p yp l o t as p l t
# Some datadata = [ [ ] , [ ] ]f o r i i n x range ( l e n ( data ) ) :
data [ i ] = numpy . abs (numpy . random . s t anda rd no rma l ( 30 ) )
# Show an h i s tog raml a b e l s = range ( l e n ( data [ 0 ] ) )
p l t . barh ( l a b e l s , data [ 0 ] , c o l o r=’#4682B4 ’ )p l t . barh ( l a b e l s , −1 ∗ data [ 1 ] , c o l o r=’#FF4500 ’ )p l t . show ( )
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 34/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Histograms
A variant to show 2 quantities per item on 1 figure
3 2 1 0 1 2 30
5
10
15
20
25
30
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 34/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
LabelsVery often, we need to name items on an histogram
impor t numpyimpor t numpy . randomimpor t ma t p l o t l i b . p yp l o t as p l t
# Some datanames = [ ’Wang Bu ’ , ’ Cheng Cao ’ , ’ Zhang Xue L i ’ , ’ Tang Wei ’ , ’ Sun Wu Kong ’ ]marks = 7 ∗ numpy . random . random sample ( l e n ( names ) ) + 3
# Ax i s s e tupf i g = p l t . f i g u r e ( )a x i s = f i g . a dd subp l o t (111)
a x i s . s e t x l i m (0 , 10)
p l t . y t i c k s ( numpy . a range ( l e n ( marks ) )+0.5 , names )
a x i s . s e t t i t l e ( ’ Data−mining marks ’ )a x i s . s e t x l a b e l ( ’mark ’ )
# Show an h i s tog ramp l t . barh ( range ( l e n ( marks ) ) , marks , c o l o r=’#4682B4 ’ )p l t . show ( )
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 35/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Labels
Very often, we need to name items on an histogram
0 2 4 6 8 10mark
Wang Bu
Cheng Cao
Zhang Xue Li
Tang Wei
Sun Wu Kong
Data-mining marks
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 35/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Error barsError bars, to indicate the accuracy of values
impor t numpyimpor t numpy . randomimpor t ma t p l o t l i b . p yp l o t as p l t
# Some datanames = [ ’ 6809 ’ , ’ 6502 ’ , ’ 8086 ’ , ’ Z80 ’ , ’RCA1802 ’ ]speed = 70 ∗ numpy . random . random sample ( l e n ( names ) ) + 30e r r o r = 9 ∗ numpy . random . random sample ( l e n ( names ) ) + 1
# Ax i s s e tupf i g = p l t . f i g u r e ( )a x i s = f i g . a dd subp l o t (111)
p l t . y t i c k s ( numpy . a range ( l e n ( names ))+0.5 , names )
a x i s . s e t t i t l e ( ’ 8 b i t s CPU benchmark − s t r i n g t e s t ’ )a x i s . s e t x l a b e l ( ’ s c o r e ’ )
# Show an h i s tog ramp l t . barh ( range ( l e n ( names ) ) , speed , x e r r=e r r o r , c o l o r=’#4682B4 ’ )p l t . show ( )
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 36/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Error bars
Error bars, to indicate the accuracy of values
0 20 40 60 80 100score
6809
6502
8086
Z80
RCA1802
8 bits CPU benchmark - string test
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 36/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Table of Contents
1 First steps
2 Curve plots
3 Scatter plots
4 Boxplots
5 Histograms
6 Usage example
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 37/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Old Faithful
Let’s display some real data: Old Faithful geyser
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 38/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Old Faithful
This way works, but good example of half-done job
impor t numpyimpor t ma t p l o t l i b . p yp l o t as p l t
# Read the datadata = numpy . l o a d t x t ( ’ . / d a t a s e t s / g e y s e r . dat ’ )
# Ax i s s e tupf i g = p l t . f i g u r e ( )a x i s = f i g . a dd subp l o t (111 , a sp e c t=’ equa l ’ )
# P lo t the p o i n t sp l t . s c a t t e r ( data [ : , 0 ] , data [ : , 1 ] , c=’#FF4500 ’ )p l t . show ( )
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 39/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 39/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Old Faithful
Let’s make a more readable figure
impor t numpyimpor t ma t p l o t l i b . p yp l o t as p l t
# Read the datadata = numpy . l o a d t x t ( ’ . / d a t a s e t s / g e y s e r . dat ’ )
# Ax i s s e tupf i g = p l t . f i g u r e ( )a x i s = f i g . a dd subp l o t (111)
a x i s . s e t t i t l e ( ’ Old F a i t h f u l g e y s e r d a t a s e t ’ )a x i s . s e t x l a b e l ( ’ w a i t i n g t ime (mn . ) ’ )a x i s . s e t y l a b e l ( ’ e r u p t i o n du r a t i o n (mn . ) ’ )
# P lo t the p o i n t sp l t . s c a t t e r ( data [ : , 0 ] , data [ : , 1 ] , c=’#FF4500 ’ )p l t . show ( )
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 40/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Old Faithful
Let’s make a more readable figure
30 40 50 60 70 80 90 100waiting time (mn.)
1.5
2.0
2.5
3.0
3.5
4.0
4.5
5.0
5.5eru
pti
on d
ura
tion (
mn.)
Old Faithful geyser dataset
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 40/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Mercury & fishes
Let’s display more complex data: fishes and mercurypoisoning
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 41/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Mercury & fishes
A first try
impor t numpyimpor t ma t p l o t l i b . p yp l o t as p l t
# Read the datadata = numpy . l o a d t x t ( ’ . / d a t a s e t s / f i s h . dat ’ )
# Ax i s s e tupf i g = p l t . f i g u r e ( )a x i s = f i g . a dd subp l o t (111)
a x i s . s e t t i t l e ( ’ North Ca r o l i n a f i s h e s mercury c o n c e n t r a t i o n ’ )a x i s . s e t x l a b e l ( ’ we ight ( g . ) ’ )a x i s . s e t y l a b e l ( ’ mercury c o n c e n t r a t i o n (ppm) ’ )
# P lo t the p o i n t sp l t . s c a t t e r ( data [ : , 3 ] , data [ : , 4 ] , c=’#FF4500 ’ )p l t . show ( )
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 42/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Mercury & fishes
A first try
1000 0 1000 2000 3000 4000 5000weight (g.)
0.5
0.0
0.5
1.0
1.5
2.0
2.5
3.0
3.5
4.0m
erc
ury
conce
ntr
ati
on (
ppm
)North Carolina fishes mercury concentration
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 42/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Mercury & fishes
Show more information with subplots
20 30 40 50 60 70length (cm.)
10000
10002000300040005000
weig
ht
(g.)
1000 0 1000 2000 3000 4000 5000weight (g.)
0.50.00.51.01.52.02.53.03.54.0
merc
ury
conce
ntr
ati
on (
ppm
)
20 30 40 50 60 70length (cm.)
0.50.00.51.01.52.02.53.03.54.0
merc
ury
conce
ntr
ati
on (
ppm
)North Carolina fishes mercury concentration
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 43/44
UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA SCHOOL OF SOFTWARE ENGINEERING OF USTC
Mercury & fishes
Data from one river with its own color
20 30 40 50 60 70length (cm.)
10000
10002000300040005000
weig
ht
(g.)
1000 0 1000 2000 3000 4000 5000weight (g.)
0.50.00.51.01.52.02.53.03.54.0
merc
ury
conce
ntr
ati
on (
ppm
)
20 30 40 50 60 70length (cm.)
0.50.00.51.01.52.02.53.03.54.0
merc
ury
conce
ntr
ati
on (
ppm
)North Carolina fishes mercury concentration
Devert Alexandre (School of Software Engineering of USTC) — Matplotlib — Slide 44/44
1 Introductionmatplotlib is probably the single most used Python package for 2D-graphics. It provides both a very quickway to visualize data from Python and publication-quality figures in many formats. We are going to explorematplotlib in interactive mode covering most common cases. We also look at the class library which isprovided with an object-oriented interface.
2 IPythonIPython is an enhanced interactive Python shell that has lots of interesting features including named inputsand outputs, access to shell commands, improved debugging and many more. When we start it with thecommand line argument -pylab, it allows interactive matplotlib sessions that hasMatlab/Mathematica-like functionality.
3 pylabpylab provides a procedural interface to the matplotlib object-oriented plotting library. It is modeledclosely after Matlab(TM). Therefore, the majority of plotting commands in pylab has Matlab(TM) analogswith similar arguments. Important commands are explained with interactive examples.
4 Simple PlotsLet's start an interactive session:
$python ipython.py -pylab
This brings us to the IPython prompt:
IPython 0.8.1 -- An enhanced Interactive Python.? -> Introduction to IPython's features.%magic -> Information about IPython's 'magic' % functions.help -> Python's own help system.object? -> Details about 'object'. ?object also works, ?? prints more.
Welcome to pylab, a matplotlib-based Python environment.For more information, type 'help(pylab)'.
In [1]:
Now we can make our first, really simple plot:
In [1]: plot(range(10))Out[1]: [<matplotlib.lines.Line2D instance at 0x01AA26E8>]
There are many line styles that can be specified with symbols:
Symbol Description
- solid line
-- dashed line
-. dash-dot line
: dotted line
. points
, pixels
o circle symbols
^ triangle up symbols
v triangle down symbols
< triangle left symbols
> triangle right symbols
s square symbols
+ plus symbols
x cross symbols
D diamond symbols
d thin diamond symbols
1 tripod down symbols
2 tripod up symbols
3 tripod left symbols
4 tripod right symbols
h hexagon symbols
H rotated hexagon symbols
p pentagon symbols
| vertical line symbols
_ horizontal line symbols
steps use gnuplot style 'steps' # kwarg only
Colors can be given in many ways: one-letter abbreviations, gray scale intensity from 0 to 1, RGB in hex andtuple format as well as any legal html color name.
The one-letter abbreviations are very handy for quick work. With following you can get quite a few thingsdone:
Abbreviation Color
b blue
g green
r red
c cyan
m magenta
y yellow
k black
w white
Other objects also have properties. The following table list the text properties:
Property Value
alpha alpha transparency on 0-1 scale
color matplotlib color arg
family set the font family, eg 'sans-serif', 'cursive', 'fantasy'
fontangle the font slant, one of 'normal', 'italic', 'oblique'
horizontalalignment 'left', 'right' or 'center'
multialignment 'left', 'right' or 'center' only for multiline strings
name font name, eg, 'Sans', 'Courier', 'Helvetica'
position x,y location
variant font variant, eg 'normal', 'small-caps'
rotation angle in degrees for rotated text
size fontsize in points, eg, 8, 10, 12
style font style, one of 'normal', 'italic', 'oblique'
text set the text string itself
verticalalignment 'top', 'bottom' or 'center'
weight font weight, e.g. 'normal', 'bold', 'heavy', 'light'
5.1 Exercise
1. Apply different line styles to a plot. Change line color and thickness as well as the size and the kind ofthe marker. Experiment with different styles.
6 TextWe'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')
matplotlib supports TeX mathematical expression. So r'$\pi$' will show up as:¼\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 tocustomize the position of the text. The arguments textcoords and xycoords specifies what x and ymean:
argument coordinate system
'figure points' points from the lower left corner of the figure
'figure pixels' pixels from the lower left corner of the figure
'figure fraction' 0,0 is lower left of figure and 1,1 is upper, right
'axes points' points from lower left corner of axes
'axes pixels' pixels from lower left corner of axes
'axes fraction' 0,1 is lower left of axes and 1,1 is upper right
'data' use the axes data coordinate system
If we do not supply xycoords, the text will be written at xy.
Furthermore, we can use an arrow whose appearance can also be described in detail:
In [14]: plot(arange(10))Out[14]: [<matplotlib.lines.Line2D instance at 0x01BB15D0>]
In [15]: ax = gca()
In [16]: ax.annotate('Here is something special', xy = (2, 1), xytext=(1,5))Out[16]: <matplotlib.text.Annotation instance at 0x01BB1648>
In [17]: ax.annotate('Here is something special', xy = (2, 1), xytext=(1,5), ....: arrowprops={'facecolor': 'r'})
6.1 Exercise
1. Annotate a line at two places with text. Use green and red arrows and align it according to figurepoints and data.
7 Ticks
7.1 Where and WhatWell formated ticks are an important part of publishing-ready figures. matplotlib provides a totallyconfigurable system for ticks. There are tick locators to specify where ticks should appear and tick formattersto make ticks look like the way you want. Major and minor ticks can be located and formated independentlyfrom each other. Per default minor ticks are not shown, i.e. there is only an empty list for them because it is asNullLocator (see below).
7.2 Tick LocatorsThere are several locators for different kind of requirements:
Class Description
NullLocator no ticks
IndexLocator locator for index plots (e.g. where x = range(len(y))
LinearLocator evenly spaced ticks from min to max
LogLocator logarithmically ticks from min to max
MultipleLocator ticks and range are a multiple of base; either integer or float
AutoLocator choose a MultipleLocator and dynamically reassign
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 themonth name.
8 Figures, Subplots, and Axes
8.1 The HierarchySo far we have used implicit figure and axes creation. This is handy for fast plots. We can have more controlover the display using figure, subplot, and axes explicitly. A figure in matplotlib means the wholewindow in the user interface. Within this figure there can be subplots. While subplot positions the plotsin a regular grid, axes allows free placement within the figure. Both can be useful depending on yourintention. We've already work with figures and subplots without explicitly calling them. When we call plotmatplotlib calls gca() to get the current axes and gca in turn calls gcf() to get the current figure. Ifthere is none it calls figure() to make one, strictly speaking, to make a subplot(111). Let's look at thedetails.
8.2 FiguresA figure is the windows in the GUI that has "Figure #" as title. Figures are numbered starting from 1 asopposed to the normal Python way starting from 0. This is clearly MATLAB-style. There are severalparameters that determine how the figure looks like:
Argument Default Description
num 1 number of figure
figsize figure.figsize figure size in in inches (width, height)
dpi figure.dpi resolution in dots per inch
facecolor figure.facecolor color of the drawing background
edgecolor figure.edgecolor color of edge around the drawing background
The defaults can be specified in the resource file and will be used most of the time. Only the number of thefigure 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 canclose 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 asargument).
As with other objects, you can set figure properties also setp or with the set_something methods.
8.3 SubplotsWith subplot you can arrange plots in regular grid. You need to specify the number of rows and columnsand the number of the plot.
A plot with two rows and one column is created with subplot(211) and subplot(212). The result lookslike this:
If you want two plots side by side, you create one row and two columns with subplot(121) andsubplot(112). The result looks like this:
You can arrange as many figures as you want. A two-by-two arrangement can be created withsubplot(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,
is_last_row, is_last_col. These can help to set ticks and labels only for the outer pots.
8.4 AxesAxes are very similar to subplots but allow placement of plots at any location in the figure. So if we want to puta 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.
9 Other Types of Plots
9.1 Many MoreSo far we have used only line plots. matplotlib offers many more types of plots. We will have a brief lookat some of them. All functions have many optional arguments that are not shown here.
9.2 Bar ChartsThe function bar creates a new bar chart:
bar([1, 2, 3], [4, 3, 7])
Now we have three bars starting at 1, 2, and 3 with height of 4, 3, 7 respectively:
The default column width is 0.8. It can be changed with common methods by setting width. As it can becolor and bottom, we can also set an error bar with yerr or xerr.
9.3 Horizontal Bar ChartsThe function barh creates an vertical bar chart. Using the same data:
barh([1, 2, 3], [4, 3, 7])
We get:
9.4 Broken Horizontal Bar ChartsWe can also have discontinuous vertical bars with broken_barh. We specify start and width of the range iny-direction and all start-width pairs in x-direction:
9.5 Box and Whisker PlotsWe can draw box and whisker plots:
boxplot((arange(2, 10), arange(1, 5)))
We want to have the whiskers well within the plot and therefore increase the y axis:
ax = gca()ax.set_ylim(0, 12)draw()
Our plot looks like this:
The range of the whiskers can be determined with the argument whis, which defaults to 1.5. The range of thewhiskers is between the most extreme data point within whis*(75%-25%) of the data.
9.6 Contour PlotsWe can also do contour plots. We define arrays for the x and y coordinates:
9.10 Polar PlotsPolar plots are also possible. Let's define our r from 0 to 360 and our theta from 0 to 360 degrees. Weneed to convert them to radians:
r = arange(360)theta = r / (180/pi)
Now plot in polar coordinates:
polar(theta, r)
We get a nice spiral:
9.11 Arrow PlotsPlotting arrows in 2D plane can be achieved with quiver. We define the x and y coordinates of the arrowshafts:
x = y = arange(10)
The x and y components of the arrows are specified as 2D arrays:
All arrows point to the upper right, except two. The one at the location (4, 4) has 3 units in x-direction and theother at location (1, 1) has -1 unit in y direction:
9.12 Scatter PlotsScatter plots print x vs. y diagrams. We define x and y and make some point in y random:
The three different values for y break out of the line:
9.13 Sparsity Pattern PlotsWorking with sparse matrices, it is often of interest as how the matrix looks like in terms of sparsity. We takean identity matrix as an example:
We reuse our data from the scatter plot (see above):
yarray([0, 7, 2, 3, 2, 5, 6, 7, 3, 9])
Now we can plot the dates at the x axis:
plot_date(dates, y)
10 The Class LibrarySo far we have used the pylab interface only. As the name suggests it is just wrapper around the classlibrary. All pylabb commands can be invoked via the class library using an object-oriented approach.
10.1 The Figure ClassThe class Figure lives in the module matplotlib.figure. Its constructor takes these arguments:
Figure provides lots of methods, many of them have equivalents in pylab. The methods add_axes andadd_subplot are called if new axes or subplot are created with axes or subplot in pylab. Also themethod gca maps directly to pylab as do legend, text and many others.
There are also several set_something method such as set_facecolor or set_edgecolor that will becalled through pylab to set properties of the figure. Figure also implements get_something methodssuch as get_axes or get_facecolor to get properties of the figure.
10.2 The Classes Axes and SubplotIn the class Axes we find most of the figure elements such as Axis, Tick, Line2D, or Text. It also sets thecoordinate system. The class Subplot inherits from Axes and adds some more functionality to arrange theplots in a grid.
Analogous to Figure, it has methods to get and set properties and methods already encountered asfunctions in pylab such as annotate. In addition, Axes has methods for all types of plots shown in theprevious section.
10.3 Other ClassesOther classes such as text, Legend or Ticker are setup very similarly. They can be understood mostlyby comparing to the pylab interface.
10.4 ExampleLet's look at an example for using the object-oriented API:
Since we are not in the interactive pylab-mode, we need to import pyplot (#1). We set the size of our figureto be 8 by 5 inches (#2). Now we initialize a new figure (#3) and add a subplot to the figure (#4). The 111says one plot at position 1, 1 just as in MATLAB. We create a new plot with the numbers from 0 to 9 and atthe same time get a reference to our line (#5). We can add several things to our plot. So we set a title andlabels for the x and y axis (#6). We also want to see the grid (#7) and would like to have little filled circles asmarkers (#8).
Finally, we save our figure as a PNG file specifying the desired resolution in dpi (#9) and show our figure onthe 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 andsquare with a legend in it.
11 Creating New Plot TypesWe can come up with our own new plot type. Matplotlib provides all the building blocks to compose new plots.We would like to have a stacked plot, that is lines are drawn on top of each other and the y values representthe difference to the other plot rather than to zero. We implement our own plot type. It assumes all y valuesare positive and the lowest plot will be filled starting from zero, i.e the x axis.
"""A simple stacked plot."""
import matplotlib as mpl #1import matplotlib.pyplot as plt #2import numpy #3
First we need to import matplotlib itself (#1) as well as pyplot (#2) and numpy (#3).
StackPlot(object): #4 """A stacked plot.
Data are stacked on top of each other and the space between the lines is filled with color. """
def __init__(self, x, y_data, axes=None, colors=None, names=None, loc='best'): #5 """Show the plot. """ # We've got plenty of default arguments. # pylint: disable-msg=R0913 # Make sure we can work with numpy arrays. self.x = numpy.array(x) #6 self.y_data = numpy.array(y_data) self.axes = axes if not axes: self.axes = plt.gca() #7 default_colors = ['r', 'b', 'g', 'y', 'm', 'w'] #8 self.colors = colors self.names = names self.loc = loc if not self.colors: #9 # Repeat colors as needed.
Now we define a new class StackedPlot (#4). It takes the x values and a two dimensional array of yvalues, 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 wassupplied, we just get the current axes (#7). This is consistent with the behavior of the standard plots. Wedefine some default colors that will be used if none are given in the __init__ (#8). We don't know howmany plots might be stacked. Therefore, we just repeat the default colors again and again until all data have acolor (#9).
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 upperboundary as well as through our colors (#15). We fill the space between both plots (#16) and also plot theline in black (#17). Furthermore, we make a nice legend for all colors (#18).
def __getattr__(self, name): #19 """Delegate not found attributes to axes.
""" 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 theset_<property> methods without actually implementing them.
Finally, we test our program (#20). We create data for x (#22) and y (#23). We replace some data in y, that isonly ones, with some different numbers. Now we can add our new stacked plot to our axes (#23). Thisargument is optional. Leaving it out, the class will use the current axes. We set some properties (#24), createthe 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 ofstacked 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 AnimationsAnimations can be useful to show how things change over time. There are two basic ways to produce ananimation with matplotlib:
1. Saving pixel graphics for each animation frame and stitch them together with tools like imagemagick orffmpeg.
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'shave a look at an example:
"""Animation with matplolib."""
import random
import matplotlib #1matplotlib.use('TkAgg') #2import matplotlib.pyplot as plt
After importing matplotlib (#1), we need to tell what GUI to use. We use TKinter here (#2). Othersupported tool kits are Qt, wxPython and GTK. The solution presented here is Tkinter specific.
class Residual(object): #3 """Generator for residual values.
This somehow mimics the residual a solver for system of equations would produces. """
The class Residual (#3) will be used as function that keeps its state. It is a placeholder for a numericalprogram 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 ofkeeping state information in self. If we are done, the limit was reached, we return None (#6). The differenceleading 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 orequal to the limit, we are done (#10).
class ResidualGraph(object): #11 """Semilog plot with matplotlib.
This plot is updated for every calculation time step. Therefore it grows dynamically. """
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 faronly value for x (#15). The value makes the first entry in our y values (#16). After initializing the graph, weneed 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 drawour 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
The update-method (#21) is important, because it will redraw the graph for each animation step. Afterincrementing the counter (#22) and adding it to the x values (#23), we append the new residual value toour y values (#24) and make new plot (#25) that we draw (#26).
def start_animation(start, limit): #27 """Start the animation. """
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 byTkinter after a specified amount of time. We get the new value for the residual from get_residual(), whichis an instance of the class Residual (#34). As long as we have a value for the residual (#30), we callwindow.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 ofResidualGraph (#35). The first call to window.after (#36) starts the animation. We also want the plotstay 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.
Matplotlib from the Shell Using matplotlib in the shell, we see the objects produced
>>> from pylab import *
>>> plot([1,2,3,4])[<matplotlib.lines.Line2D object at 0x01A3EF30>]
>>> show()
Hangs until the figure is dismissed or close() issued
show() clears the figure: issuing it again has no result
If you construct another figure, show() displays it without hanging
draw()
Clear the current figure and initialize a blank figure without hanging or displaying anything
Results of subsequent commands added to the figure >>> draw()
>>> plot([1,2,3])[<matplotlib.lines.Line2D object at 0x01C4B6B0>]
>>> plot([1,2,3],[0,1,2])[<matplotlib.lines.Line2D object at 0x01D28330>]
Shows 2 lines on the figure after show() is invoked
Use close() to dismiss the figure and clear it
clf() clears the figure (also deletes the white background) without dismissing it
Figures saved using savefig() in the shell by default are saved in C:\Python27
Override this by giving a full pathname
Interactive Mode In interactive mode, objects are displayed as soon as they’re created
Use ion() to turn on interactive mode, ioff() to turn it off
Example Import the pylab namespace, turn on interactive mode and plot a line
>>> from pylab import *
>>> ion()
>>> plot([1,2,3,4])[<matplotlib.lines.Line2D object at 0x02694A10>]
A figure appears with this line on it
The command line doesn’t hang
Plot another line >>> plot([4,3,2,1])[<matplotlib.lines.Line2D object at 0x00D68C30>]
This line is shown on the figure as soon as the command is issued
Turn off interactive mode and plot another line>>> ioff()
>>> plot([3,2.5,2,1.5])[<matplotlib.lines.Line2D object at 0x00D09090>]
The figure remains unchanged—no new line
Update the figure >>> show()
The figure now has all 3 lines
The command line is hung until the figure is dismissed
Matplotlib in IPython Recommended way to use matplotlib interactively is with ipython
In the IPython drop-down menu, select pylab
IPython’s pylab mode detects the matplotlibrc file
Makes the right settings to run matplotlib with your GUI of choice in interactive mode using threading
IPython in pylab mode knows enough about matplotlib internals to make all the right settings
Plots items as they’re produce without draw() or show()
Default folder for saving figures on fileC:\Documents and Settings\current user
More Curve Plotting plot() has a format string argument specifying the color and line type
Goes after the y argument (whether or not there’s an x argument)
Can specify one or the other or concatenate a color string with a line style string in either order
Default format string is 'b-' ( solid blue line)
Some color abbreviations b (blue), g (green), r (red), k (black), c (cyan)
Can specify colors in many other ways (e.g., RGB triples)
Some line styles that fill in the line between the specified points - (solid line), -- (dashed line), -. (dash-dot line), : (dotted line)
Some line styles that don’t fill in the line between the specified point o (circles), + (plus symbols), x (crosses), D (diamond symbols)
For a complete list of line styles and format strings, http://matplotlib.sourceforge.net/api/pyplot_api.html#module-matplotlib.pyplot
Look under matplotlib.pyplot.plot()
The axis() command takes a list[xmin, xmax, ymin, ymax]
and specifies the view port of the axes
ExampleIn [1]: plot([1,2,3,4], [1,4,9,16], 'ro')Out[1]: [<matplotlib.lines.Line2D object at 0x034579D0>]
In [2]: axis([0, 6, 0, 20])Out[2]: [0, 6, 0, 20]
Generally work with arrays, not lists
All sequences are converted to NumPy arrays internally
plot() can draw more than one line at a time
Put the (1-3) arguments for one line in the figure
Then the (1-3) arguments for the next line
And so on
Can’t have 2 lines in succession specified just by their y arguments
matplotlib couldn’t tell whether it’s a y then the next y or an x then the corresponding y
The example (next slide) illustrates plotting several lines with different format styles in one command using arrays
In [1]: t = arange(0.0, 5.2, 0.2)
In [2]: plot(t, t, t, t**2, 'rs', t, t**3, 'g^')Out[2]:[<matplotlib.lines.Line2D object at 0x02A6D410>,<matplotlib.lines.Line2D object at 0x02A6D650>,<matplotlib.lines.Line2D object at 0x02A6D8D0>]
Lines you encounter are instances of the Line2D class
Instances have the following properties
Property Values
alpha The alpha transparency on 0-1 scale
antialiased True | False - use antialised rendering
color a matplotlib color arg
label a string optionally used for legend
linestyle One of -- : -. -
linewidth a float, the line width in points
marker One of + , o . s v x > < ^
markeredgewidth The line width around the marker symbol
markeredgecolor The edge color if a marker is used
markerfacecolor The face color if a marker is used
markersize The size of the marker in points
alpha = 0.0 is complete transparency, alpha = 1.0 is complete opacity
One of 3 ways to set line properties: Use keyword line properties as keyword arguments—e.g., plot(x, y, linewidth=2.0)
Another way: Use the setter methods of the Line2D instance
For every property prop, there’s a set_prop() method that takes an update value for that property
To use this, note that plot() returns a list of lines
The following has 1 line with tuple unpacking line, = plot(x, y, 'o')
line.set_antialiased(False)
The 3rd (and final) way to set line properties: use function setp()
1st argument can be an object or sequence of objects
The rest are keyword arguments updating the object’s (or objects’) properties
E.g., lines = plot(x1, y1, x2, y2)
setp(lines, color='r', linewidth=2.0)
Text The text commands are self-explanatory
xlabel()
ylabel()
title()
text()
In simple cases, all but text() take just a string argument
text() needs at least 3 arguments: x and y coordinates (as per the axes) and a string
All take optional keyword arguments or dictionaries to specify the font properties
Most of the arguments may be either scalars or sequences
Say in […] what they normally are
Required argumentsleft x coordinates of the left sides of the bars [sequence]height height of the bars [sequence]
Some optional keyword arguments orientation orientation of the bars (‘vertical’ | ‘horizontal’)width width of the bars [scalar]color color of the bars [scalar]xerr if not None,
generates error ticks on top the bars [sequence]yerr if not None,
generates errorbars on top the bars [sequence]ecolor color of any errorbar [scalar]capsize length (in points) of errorbar caps (default 2) [scalar]log False (default) leaves the orientation axis as is,
True sets it to log scale
For the following example, need the following
yticks(seq, labels)
Make the ticks on the y axis at y-values in seq with labels the corresponding elements of labels