Beruflich Dokumente
Kultur Dokumente
Matplotlib
A tutorial
Devert Alexandre
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
Curve plot
Let’s plot a curve
i m p o r t math
import m a t p l o t l i b . pyplot as p l t
# Generate a s i n u s o i d
n b S a m p l e s = 256
xRange = (−math . p i , math . p i )
x , y = [] , []
f o r n i n xrange ( nbSamples ) :
k = ( n + 0 . 5 ) / nbSamples
x . append ( xRange [ 0 ] + ( xRange [ 1 ] − xRange [ 0 ] ) ∗ k )
y . append ( math . s i n ( x [ − 1 ] ) )
# Plot the s i n u s o i d
plt . plot (x , y)
p l t . show ( )
Curve plot
This will show you something like this
1.0
0.5
0.0
0.5
1.0 4 3 2 1 0 1 2 3 4
numpy
matplotlib can work with numpy arrays
i m p o r t math
i m p o r t numpy
import m a t p l o t l i b . pyplot as p l t
# Generate a s i n u s o i d
n b S a m p l e s = 256
xRange = (−math . p i , math . p i )
x , y = numpy . z e r o s ( n b S a m p l e s ) , numpy . z e r o s ( n b S a m p l e s )
f o r n i n xrange ( nbSamples ) :
k = ( n + 0 . 5 ) / nbSamples
x [ n ] = xRange [ 0 ] + ( xRange [ 1 ] − xRange [ 0 ] ) ∗ k
y [ n ] = math . s i n ( x [ n ] )
# Plot the s i n u s o i d
plt . plot (x , y)
p l t . show ( )
numpy
• zeros build arrays filled of 0
• linspace build arrays filled with an arithmetic
sequence
i m p o r t math
i m p o r t numpy
import m a t p l o t l i b . pyplot as p l t
# Generate a s i n u s o i d
x = numpy . l i n s p a c e (−math . p i , math . p i , num=256)
y = numpy . z e r o s ( n b S a m p l e s )
f o r n i n xrange ( nbSamples ) :
y [ n ] = math . s i n ( x [ n ] )
# Plot the s i n u s o i d
plt . plot (x , y)
p l t . show ( )
numpy
i m p o r t math
i m p o r t numpy
import m a t p l o t l i b . pyplot as p l t
# Generate a s i n u s o i d
x = numpy . l i n s p a c e (−math . p i , math . p i , num=256)
y = numpy . s i n ( x )
# Plot the s i n u s o i d
plt . plot (x , y)
p l t . show ( )
PDF output
i m p o r t math
i m p o r t numpy
import m a t p l o t l i b . pyplot as p l t
# Generate a s i n u s o i d
x = numpy . l i n s p a c e (−math . p i , math . p i , num=256)
y = numpy . s i n ( x )
# Plot the s i n u s o i d
plt . plot (x , y)
p l t . s a v e f i g ( ’ s i n −p l o t . p d f ’ , t r a n s p a r e n t=True )
Table of Contents
1 First steps
2 Curve plots
3 Scatter plots
4 Boxplots
5 Histograms
6 Usage example
Multiple curves
i m p o r t math
i m p o r t numpy
import m a t p l o t l i b . pyplot as p l t
# G e n e r a t e a s i n u s o i d and a c o s i n o i d
x = 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 . c o s ( x )
Multiple curves
It’s often convenient to show several curves in one figure
1.0
0.5
0.0
0.5
1.0 4 3 2 1 0 1 2 3 4
Custom colors
i m p o r t math
i m p o r t numpy
import m a t p l o t l i b . pyplot as p l t
# G e n e r a t e a s i n u s o i d and a c o s i n o i d
x = 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 . c o s ( x )
Custom colors
Changing colors can help to make nice documents
1.0
0.5
0.0
0.5
1.0 4 3 2 1 0 1 2 3 4
Line thickness
i m p o r t math
i m p o r t numpy
import m a t p l o t l i b . pyplot as p l t
# G e n e r a t e a s i n u s o i d and a c o s i n o i d
x = 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 . c o s ( x )
Line thickness
Line thickness can be changed as well
1.0
0.5
0.0
0.5
1.0 4 3 2 1 0 1 2 3 4
Line patterns
i m p o r t math
i m p o r t numpy
import m a t p l o t l i b . pyplot as p l t
# L i n e s t y l e s can be ’ − ’ , ’−−’, ’ − . ’ , ’: ’
# G e n e r a t e a s i n u s o i d and a c o s i n o i d
x = 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 . c o s ( x )
Line patterns
For printed document, colors can be replaced by line
patterns
1.0
0.5
0.0
0.5
1.0 4 3 2 1 0 1 2 3 4
Markers
i m p o r t math
i m p o r t numpy
import m a t p l o t l i b . pyplot as p l t
# G e n e r a t e a s i n u s o i d and a c o s i n o i d
x = 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 . c o s ( x )
Markers
It sometime relevant to show the data points
1.0
0.5
0.0
0.5
1.0 4 3 2 1 0 1 2 3 4
Legend
i m p o r t math
i m p o r t numpy
import m a t p l o t l i b . pyplot as p l t
# G e n e r a t e a s i n u s o i d and a c o s i n o i d
x = 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 . c o s ( x )
Legend
A legend can help to make self–explanatory figures
1.0
sin(x)
cos(x)
0.5
0.0
0.5
1.0 4 3 2 1 0 1 2 3 4
i m p o r t math
i m p o r t numpy
import m a t p l o t l i b . pyplot as p l t
# G e n e r a t e a s i n u s o i d and a c o s i n o i d
x = 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 . c o s ( x )
# Axis setup
f i g = plt . figure ()
a x i s = f i g . add subplot (111)
1.5
sin(x)
cos(x)
1.0
0.5
0.0
0.5
1.0
1.5
4 3 2 1 0 1 2 3 4
Grid
Same goes for a grid, can be helpful
i m p o r t math
i m p o r t numpy
import m a t p l o t l i b . pyplot as p l t
# G e n e r a t e a s i n u s o i d and a c o s i n o i d
x = 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 . c o s ( x )
# Axis setup
f i g = plt . figure ()
a x i s = f i g . add subplot (111)
Grid
Same goes for a grid, can be helpful
1.5
sin(x)
cos(x)
1.0
0.5
0.0
0.5
1.0
1.5
4 3 2 1 0 1 2 3 4
Error bars
Your data might come with a known measure error
import math
import numpy
import numpy . random
import m a t p l o t l i b . pyplot as p l t
# Generate a noisy s i n u s o i d
x = numpy . l i n s p a c e (−math . p i , math . p i , num=48)
y = numpy . s i n ( x + 0 . 0 5 ∗ numpy . random . s t a n d a r d n o r m a l ( l e n ( x ) ) )
y e r r o r = 0 . 1 ∗ numpy . random . s t a n d a r d n o r m a l ( l e n ( x ) )
# Axis setup
f i g = plt . figure ()
a x i s = f i g . add subplot (111)
Error bars
Your data might come with a known measure error
1.5
1.0
0.5
0.0
0.5
1.0
1.5
4 3 2 1 0 1 2 3 4
Table of Contents
1 First steps
2 Curve plots
3 Scatter plots
4 Boxplots
5 Histograms
6 Usage example
Scatter plot
A scatter plot just shows one point for each dataset entry
i m p o r t numpy
i m p o r t numpy . random
import m a t p l o t l i b . pyplot as p l t
# G e n e r a t e a 2d n o r m a l d i s t r i b u t i o n
n b P o i n t s = 100
x = numpy . random . s t a n d a r d n o r m a l ( n b P o i n t s )
y = numpy . random . s t a n d a r d n o r m a l ( n b P o i n t s )
Scatter plot
A scatter plot just shows one point for each dataset entry
5
4
3
2
1
0
1
2
3
43 2 1 0 1 2 3
Aspect ratio
If can be very important to have the same scale on both
axis
i m p o r t numpy
i m p o r t numpy . random
import m a t p l o t l i b . pyplot as p l t
# G e n e r a t e a 2d n o r m a l d i s t r i b u t i o n
n b P o i n t s = 100
x = numpy . random . s t a n d a r d n o r m a l ( n b P o i n t s )
y = 0 . 1 ∗ numpy . random . s t a n d a r d n o r m a l ( n b P o i n t s )
# Axis setup
f i g = plt . figure ()
a x i s = f i g . a d d s u b p l o t ( 1 1 1 , a s p e c t= ’ e q u a l ’ )
Aspect ratio
If can be very important to have the same scale on both
axis
0.3
0.2
0.1
0.0
0.1
0.2
0.3 3 2 1 0 1 2 3
Aspect ratio
Alternative way to keep the same scale on both axis
i m p o r t numpy
i m p o r t numpy . random
import m a t p l o t l i b . pyplot as p l t
# G e n e r a t e a 2d n o r m a l d i s t r i b u t i o n
n b P o i n t s = 100
x = numpy . random . s t a n d a r d n o r m a l ( n b P o i n t s )
y = 0 . 1 ∗ numpy . random . s t a n d a r d n o r m a l ( n b P o i n t s )
# Axis setup
f i g = plt . figure ()
a x i s = f i g . add subplot (111)
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 )
Aspect ratio
Alternative way to keep the same scale on both axis
2 1 0 1 2
# G e n e r a t e a 2d n o r m a l d i s t r i b u t i o n
n b P o i n t s = 100
x , y = [] , []
x += [ numpy . random . s t a n d a r d n o r m a l ( n b P o i n t s ) ]
y += [ 0 . 2 5 ∗ numpy . random . s t a n d a r d n o r m a l ( n b P o i n t s ) ]
x += [ 0 . 5 ∗ numpy . random . s t a n d a r d n o r m a l ( n b P o i n t s ) + 3 . 0 ]
y += [ 2 ∗ numpy . random . s t a n d a r d n o r m a l ( n b P o i n t s ) + 2 . 0 ]
# Axis setup
f i g = plt . figure ()
a x i s = f i g . a d d s u b p l o t ( 1 1 1 , a s p e c t= ’ e q u a l ’ )
p l t . show ( )
43 2 1 0 1 2 3 4 5
Showing centers
It can help to see the centers or the median points
i m p o r t numpy
i m p o r t numpy . random
import m a t p l o t l i b . pyplot as p l t
# G e n e r a t e a 2d n o r m a l d i s t r i b u t i o n
n b P o i n t s = 100
x , y = [] , []
x += [ numpy . random . s t a n d a r d n o r m a l ( n b P o i n t s ) ]
y += [ 0 . 2 5 ∗ numpy . random . s t a n d a r d n o r m a l ( n b P o i n t s ) ]
x += [ 0 . 5 ∗ numpy . random . s t a n d a r d n o r m a l ( n b P o i n t s ) + 3 . 0 ]
y += [ 2 ∗ numpy . random . s t a n d a r d n o r m a l ( n b P o i n t s ) + 2 . 0 ]
# Axis setup
f i g = plt . figure ()
a x i s = f i g . a d d s u b p l o t ( 1 1 1 , a s p e c t= ’ e q u a l ’ )
p l t . show ( )
Showing centers
It can help to see the centers or the median points
43 2 1 0 1 2 3 4 5
Marker styles
You can use different markers styles
i m p o r t numpy
i m p o r t numpy . random
import m a t p l o t l i b . pyplot as p l t
m a r k e r s = ( ’+ ’ , ’ ˆ ’ , ’ . ’ )
# G e n e r a t e a 2d n o r m a l d i s t r i b u t i o n
n b P o i n t s = 100
x , y = [] , []
x += [ numpy . random . s t a n d a r d n o r m a l ( n b P o i n t s ) ]
y += [ 0 . 2 5 ∗ numpy . random . s t a n d a r d n o r m a l ( n b P o i n t s ) ]
x += [ 0 . 5 ∗ numpy . random . s t a n d a r d n o r m a l ( n b P o i n t s ) + 3 . 0 ]
y += [ 2 ∗ numpy . random . s t a n d a r d n o r m a l ( n b P o i n t s ) + 2 . 0 ]
# Axis setup
f i g = plt . figure ()
a x i s = f i g . a d d s u b p l o t ( 1 1 1 , a s p e c t= ’ e q u a l ’ )
p l t . show ( )
Marker styles
You can use different markers styles
10
44 3 2 1 0 1 2 3 4 5
Table of Contents
1 First steps
2 Curve plots
3 Scatter plots
4 Boxplots
5 Histograms
6 Usage example
Boxplots
i m p o r t numpy
i m p o r t numpy . random
import m a t p l o t l i b . pyplot as p l t
# Show a b o x p l o t o f t h e d a t a
plt . boxplot (x)
p l t . show ( )
Boxplots
Let’s do a simple boxplot
3 1
Boxplots
i m p o r t numpy
i m p o r t numpy . random
import m a t p l o t l i b . pyplot as p l t
# Show a b o x p l o t o f t h e d a t a
p l t . s c a t t e r ( [ 0 ] ∗ l e n ( x ) , x , c= ’ #4682B4 ’ )
p l t . boxplot (x , p o s i t i o n s =[0])
p l t . show ( )
Boxplots
You might want to show the original data in the same
time
4 0
Multiple boxplots
Boxplots are often used to show side by side various
distributions
i m p o r t numpy
i m p o r t numpy . random
import m a t p l o t l i b . pyplot as p l t
# Show a b o x p l o t o f t h e d a t a
p l t . boxplot ( data )
p l t . show ( )
Multiple boxplots
Boxplots are often used to show side by side various
distributions
14
12
10
2 1 2 3 4 5
Orientation
i m p o r t numpy
i m p o r t numpy . random
import m a t p l o t l i b . pyplot as p l t
# Show a b o x p l o t o f t h e d a t a
p l t . b o x p l o t ( data , v e r t =0)
p l t . show ( )
Orientation
Changing the orientation is easily done
6 4 2 0 2 4 6 8 10 12
Legend
Good graphics have a proper legend
i m p o r t numpy
i m p o r t numpy . random
import m a t p l o t l i b . pyplot as p l t
# Axis setup
f i g = plt . figure ()
a x i s = f i g . add subplot (111)
# Show a b o x p l o t o f t h e d a t a
p l t . boxplot ( data )
p l t . show ( )
Legend
Good graphics have a proper legend
110
108
concentration (ppm)
106
104
102
Table of Contents
1 First steps
2 Curve plots
3 Scatter plots
4 Boxplots
5 Histograms
6 Usage example
Histograms
i m p o r t numpy
i m p o r t numpy . random
import m a t p l o t l i b . pyplot as p l t
# Some d a t a
d a t a = numpy . a b s ( numpy . random . s t a n d a r d n o r m a l ( 3 0 ) )
# Show an h i s t o g r a m
p l t . b a r h ( r a n g e ( l e n ( d a t a ) ) , data , c o l o r= ’ #4682B4 ’ )
p l t . show ( )
Histograms
Histogram are convenient to sum-up results
30
25
20
15
10
Histograms
i m p o r t numpy
i m p o r t numpy . random
import m a t p l o t l i b . pyplot as p l t
# Some d a t a
data = [ [ ] , [ ] ]
f o r i i n xrange ( l e n ( data ) ) :
d a t a [ i ] = numpy . a b s ( numpy . random . s t a n d a r d n o r m a l ( 3 0 ) )
# Show an h i s t o g r a m
l a b e l s = range ( l e n ( data [ 0 ] ) )
p l t . b a r h ( l a b e l s , d a t a [ 0 ] , c o l o r= ’ #4682B4 ’ )
p l t . b a r h ( l a b e l s , −1 ∗ d a t a [ 1 ] , c o l o r= ’#FF4500 ’ )
p l t . show ( )
Histograms
A variant to show 2 quantities per item on 1 figure
30
25
20
15
10
03 2 1 0 1 2 3
Labels
Very often, we need to name items on an histogram
i m p o r t numpy
i m p o r t numpy . random
import m a t p l o t l i b . pyplot as p l t
# Some d a t a
names = [ ’ Wang Bu ’ , ’ Cheng Cao ’ , ’ Zhang Xue L i ’ , ’ Tang Wei ’ , ’ Sun Wu Kong ’ ]
marks = 7 ∗ numpy . random . r a n d o m s a m p l e ( l e n ( names ) ) + 3
# Axis setup
f i g = plt . figure ()
a x i s = f i g . add subplot (111)
a x i s . s e t x l i m (0 , 10)
a x i s . s e t t i t l e ( ’ Data−m i n i n g marks ’ )
a x i s . s e t x l a b e l ( ’ mark ’ )
# Show an h i s t o g r a m
p l t . b a r h ( r a n g e ( l e n ( marks ) ) , marks , c o l o r= ’ #4682B4 ’ )
p l t . show ( )
Labels
Very often, we need to name items on an histogram
Data-mining marks
Sun Wu Kong
Tang Wei
Zhang Xue Li
Cheng Cao
Wang Bu
0 2 4 6 8 10
mark
Error bars
Error bars, to indicate the accuracy of values
i m p o r t numpy
i m p o r t numpy . random
import m a t p l o t l i b . pyplot as p l t
# Some d a t a
names = [ ’ 6809 ’ , ’ 6502 ’ , ’ 8086 ’ , ’ Z80 ’ , ’ RCA1802 ’ ]
s p e e d = 70 ∗ numpy . random . r a n d o m s a m p l e ( l e n ( names ) ) + 30
e r r o r = 9 ∗ numpy . random . r a n d o m s a m p l e ( l e n ( names ) ) + 1
# Axis setup
f i g = plt . figure ()
a x i s = f i g . add subplot (111)
# Show an h i s t o g r a m
p l t . b a r h ( r a n g e ( l e n ( names ) ) , s p e e d , x e r r=e r r o r , c o l o r= ’ #4682B4 ’ )
p l t . show ( )
Error bars
Error bars, to indicate the accuracy of values
Z80
8086
6502
6809
0 20 40 60 80 100
score
Table of Contents
1 First steps
2 Curve plots
3 Scatter plots
4 Boxplots
5 Histograms
6 Usage example
Old Faithful
Old Faithful
i m p o r t numpy
import m a t p l o t l i b . pyplot as p l t
# Read t h e d a t a
d a t a = numpy . l o a d t x t ( ’ . / d a t a s e t s / g e y s e r . d a t ’ )
# Axis setup
f i g = plt . figure ()
a x i s = f i g . a d d s u b p l o t ( 1 1 1 , a s p e c t= ’ e q u a l ’ )
Old Faithful
This way works, but good example of half-done job
5.5
5.0
4.5
4.0
3.5
3.0
2.5
2.0
1.530 40 50 60 70 80 90 100
Old Faithful
i m p o r t numpy
import m a t p l o t l i b . pyplot as p l t
# Read t h e d a t a
d a t a = numpy . l o a d t x t ( ’ . / d a t a s e t s / g e y s e r . d a t ’ )
# Axis setup
f i g = plt . figure ()
a x i s = f i g . add subplot (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 i m e (mn . ) ’ )
a x i s . s e t y l a b e l ( ’ e r u p t i o n d u r a t i o n (mn . ) ’ )
Old Faithful
Let’s make a more readable figure
5.0
4.5
eruption duration (mn.)
4.0
3.5
3.0
2.5
2.0
1.530 40 50 60 70 80 90 100
waiting time (mn.)
i m p o r t numpy
import m a t p l o t l i b . pyplot as p l t
# Read t h e d a t a
d a t a = numpy . l o a d t x t ( ’ . / d a t a s e t s / f i s h . d a t ’ )
# Axis setup
f i g = plt . figure ()
a x i s = f i g . add subplot (111)
a x i s . s e t t i t l e ( ’ North C a 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 ( ’ weight (g . ) ’ )
a x i s . s e t y l a b e l ( ’ m e r c u r y c o n c e n t r a t i o n ( ppm ) ’ )
3.5
3.0
mercury concentration (ppm)
2.5
2.0
1.5
1.0
0.5
0.0
0.51000 0 1000 2000 3000 4000 5000
weight (g.)
3000
2000
1000
0
(ppm)concentration (ppm)
100020 30 40 50 60 70
4.0 length (cm.)
3.5
3.0
2.5
2.0
1.5
1.0
0.5
0.0
0.51000
mercury
3.0
2.5
2.0
1.5
1.0
0.5
0.0
0.520 30 40 50 60 70
length (cm.)
3000
2000
1000
0
(ppm)concentration (ppm)
100020 30 40 50 60 70
4.0 length (cm.)
3.5
3.0
2.5
2.0
1.5
1.0
0.5
0.0
0.51000
mercury
3.0
2.5
2.0
1.5
1.0
0.5
0.0
0.520 30 40 50 60 70
length (cm.)