Aug
SEP
Oct
08
2019
2020
2021
About this capture
T h e W a y b a c k M a c h i n e - h t t p : / / w e b . a r c h i v e . o r g / w e b / 2 0 2 0 0 9 0 8 0 8 4 0 1 4 / h t t p s : / / g i t h u b . c o m / r o u g i e r / m a t p l o t l i b - t u t o r i a l
S k i p t o c o n t e n t
/ ; r e f _ c t a : S i g n u p ; r e f _ l o c : h e a d e r l o g g e d o u t " >
S i g n up
●
F e a t u r e s →
● C o d e r e v i e w
● P r o j e c t m a n a g e m e n t
● I n t e g r a t i o n s
● A c t i o n s
● P a c k a g e s
● S e c u r i t y
● T e a m m a n a g e m e n t
● H o s t i n g
● M o b i l e
● C u s t o m e r s t o r i e s →
● S e c u r i t y →
●
●
●
● E x p l o r e G i t H u b →
L e a r n & c o n t r i b u t e
● T o p i c s
● C o l l e c t i o n s
● T r e n d i n g
● L e a r n i n g L a b
● O p e n s o u r c e g u i d e s
C o n n e c t w i t h o t h e r s
● E v e n t s
● C o m m u n i t y f o r u m
● G i t H u b E d u c a t i o n
● G i t H u b S t a r s p r o g r a m
●
●
P l a n s →
● C o m p a r e p l a n s
● C o n t a c t S a l e s
● N o n p r o f i t →
● E d u c a t i o n →
In this repository
All GitHub
↵
Jump to
↵
No suggested jump to results
{ { m e s s a g e } }
●
W a t c h
1 0 3
●
S t a r
1 . 6 k
●
F o r k
5 6 8
M a t p l o t l i b t u t o r i a l f o r b e g i n n e r
1 . 6 k
s t a r s
5 6 8
f o r k s
S t a r
W a t c h
●
C o d e
●
I s s u e s
6
●
P u l l r e q u e s t s
0
●
A c t i o n s
●
P r o j e c t s
0
●
S e c u r i t y
●
I n s i g h t s
M o r e
●
C o d e
●
I s s u e s
●
P u l l r e q u e s t s
●
A c t i o n s
●
P r o j e c t s
●
S e c u r i t y
●
I n s i g h t s
D i s m i s s
J o i n G i t H u b t o d a y
G i t H u b i s h o m e t o o v e r 5 0 m i l l i o n d e v e l o p e r s w o r k i n g t o g e t h e r t o h o s t a n d r e v i e w c o d e , m a n a g e p r o j e c t s , a n d b u i l d s o f t w a r e t o g e t h e r .
S i g n u p
1
b r a n c h
1
t a g
G o t o f i l e
C o d e
C l o n e w i t h H T T P S
U s e G i t o r c h e c k o u t w i t h S V N u s i n g t h e w e b U R L .
●
O p e n w i t h G i t H u b D e s k t o p
●
D o w n l o a d Z I P
L a u n c h i n g G i t H u b D e s k t o p
I f n o t h i n g h a p p e n s , d o w n l o a d G i t H u b D e s k t o p a n d t r y a g a i n .
G o b a c k
L a u n c h i n g G i t H u b D e s k t o p
I f n o t h i n g h a p p e n s , d o w n l o a d G i t H u b D e s k t o p a n d t r y a g a i n .
G o b a c k
L a u n c h i n g X c o d e
I f n o t h i n g h a p p e n s , d o w n l o a d X c o d e a n d t r y a g a i n .
G o b a c k
L a u n c h i n g V i s u a l S t u d i o
I f n o t h i n g h a p p e n s , d o w n l o a d t h e G i t H u b e x t e n s i o n f o r V i s u a l S t u d i o a n d t r y a g a i n .
G o b a c k
L a t e s t c o m m i t
r o u g i e r
M e r g e p u l l r e q u e s t # 1 5 f r o m F r a n k l i n C h e n / f i x - p r i n t
…
5 2 9 a 2 0 3
J u n 1 5 , 2 0 2 0
M e r g e p u l l r e q u e s t # 1 5 f r o m F r a n k l i n C h e n / f i x - p r i n t
Fix print function calls by adding parentheses.
5 2 9 a 2 0 3
G i t s t a t s
●
38
c o m m i t s
F i l e s
P e r m a l i n k
F a i l e d t o l o a d l a t e s t c o m m i t i n f o r m a t i o n .
T y p e
N a m e
L a t e s t c o m m i t m e s s a g e
C o m m i t t i m e
f i g u r e s
C h a n g e d r a w i n g o r d e r
J u n 1 6 , 2 0 1 7
s c r i p t s
F i x p r i n t f u n c t i o n c a l l s b y a d d i n g p a r e n t h e s e s .
J u n 1 2 , 2 0 2 0
. g i t i g n o r e
I n i t i a l i m p o r t
M a y 1 2 , 2 0 1 3
M a k e f i l e
I n i t i a l i m p o r t
M a y 1 2 , 2 0 1 3
R E A D M E . h t m l
r e m o v e d p y l a b r e f e r e n c e
J u n 3 , 2 0 2 0
R E A D M E . r s t
r e m o v e d p y l a b r e f e r e n c e
J u n 3 , 2 0 2 0
d a n a . c s s
I n i t i a l i m p o r t
M a y 1 2 , 2 0 1 3
r e s e t . c s s
I n i t i a l i m p o r t
M a y 1 2 , 2 0 1 3
V i e w c o d e
R E A D M E . r s t
M a t p l o t l i b t u t o r i a l
N i c o l a s P . R o u g i e r
T a b l e o f C o n t e n t s
● I n t r o d u c t i o n
● S i m p l e p l o t
● F i g u r e s , S u b p l o t s , A x e s a n d T i c k s
● A n i m a t i o n
● O t h e r T y p e s o f P l o t s
● B e y o n d t h i s t u t o r i a l
● Q u i c k r e f e r e n c e s
S o u r c e s a r e a v a i l a b l e f r o m
g i t h u b
A l l c o d e a n d m a t e r i a l i s l i c e n s e d u n d e r a C r e a t i v e C o m m o n s
A t t r i b u t i o n - S h a r e A l i k e 4 . 0 .
Y o u c a n t e s t y o u r i n s t a l l a t i o n b e f o r e t h e t u t o r i a l u s i n g t h e c h e c k - i n s t a l l a t i o n . p y s c r i p t .
S e e a l s o :
● F r o m P y t h o n t o N u m p y
● 1 0 0 N u m p y e x e r c i c e s
● T e n s i m p l e r u l e s f o r b e t t e r f i g u r e s
m a t p l o t l i b i s p r o b a b l y t h e s i n g l e m o s t u s e d P y t h o n p a c k a g e f o r 2 D - g r a p h i c s . I t
p r o v i d e s b o t h a v e r y q u i c k w a y t o v i s u a l i z e d a t a f r o m P y t h o n a n d
p u b l i c a t i o n - q u a l i t y f i g u r e s i n m a n y f o r m a t s . W e a r e g o i n g t o e x p l o r e
m a t p l o t l i b i n i n t e r a c t i v e m o d e c o v e r i n g m o s t c o m m o n c a s e s .
I P y t h o n a n d t h e p y l a b m o d e
I P y t h o n i s a n e n h a n c e d i n t e r a c t i v e P y t h o n s h e l l t h a t
h a s l o t s o f i n t e r e s t i n g f e a t u r e s i n c l u d i n g n a m e d i n p u t s a n d o u t p u t s , a c c e s s t o
s h e l l c o m m a n d s , i m p r o v e d d e b u g g i n g a n d m u c h m o r e . W h e n w e s t a r t i t w i t h t h e
c o m m a n d l i n e a r g u m e n t - p y l a b ( - - p y l a b s i n c e I P y t h o n v e r s i o n 0 . 1 2 ) , i t a l l o w s
i n t e r a c t i v e m a t p l o t l i b s e s s i o n s t h a t h a v e M a t l a b / M a t h e m a t i c a - l i k e f u n c t i o n a l i t y .
p y p l o t
p y p l o t p r o v i d e s a c o n v e n i e n t i n t e r f a c e t o t h e m a t p l o t l i b o b j e c t - o r i e n t e d
p l o t t i n g l i b r a r y . I t i s m o d e l e d c l o s e l y a f t e r M a t l a b ( TM ) . T h e r e f o r e , t h e
m a j o r i t y o f p l o t t i n g c o m m a n d s i n p y p l o t h a v e M a t l a b ( TM ) a n a l o g s w i t h s i m i l a r
a r g u m e n t s . I m p o r t a n t c o m m a n d s a r e e x p l a i n e d w i t h i n t e r a c t i v e e x a m p l e s .
I n t h i s s e c t i o n , w e w a n t t o d r a w t h e c o s i n e a n d s i n e f u n c t i o n s o n t h e s a m e
p l o t . S t a r t i n g f r o m t h e d e f a u l t s e t t i n g s , w e ' l l e n r i c h t h e f i g u r e s t e p b y s t e p
t o m a k e i t n i c e r .
T h e f i r s t s t e p i s t o g e t t h e d a t a f o r t h e s i n e a n d c o s i n e f u n c t i o n s :
import numpy as np
X = np.linspace(-np.pi, np.pi, 256, endpoint=True)
C, S = np.cos(X ), np.sin(X )
X i s n o w a N u m P y a r r a y w i t h 2 5 6 v a l u e s r a n g i n g f r o m - π t o + π ( i n c l u d e d ) . C i s
t h e c o s i n e ( 2 5 6 v a l u e s ) a n d S i s t h e s i n e ( 2 5 6 v a l u e s ) .
T o r u n t h e e x a m p l e , y o u c a n d o w n l o a d e a c h o f t h e e x a m p l e s a n d r u n i t u s i n g :
$ python exercice_1.py
Y o u c a n g e t s o u r c e f o r e a c h s t e p b y c l i c k i n g o n t h e c o r r e s p o n d i n g f i g u r e .
U s i n g d e f a u l t s
D o c u m e n t a t i o n
● p l o t t u t o r i a l
● p l o t ( ) c o m m a n d
M a t p l o t l i b c o m e s w i t h a s e t o f d e f a u l t s e t t i n g s t h a t a l l o w c u s t o m i z i n g a l l
k i n d s o f p r o p e r t i e s . Y o u c a n c o n t r o l t h e d e f a u l t s o f a l m o s t e v e r y p r o p e r t y i n
m a t p l o t l i b : f i g u r e s i z e a n d d p i , l i n e w i d t h , c o l o r a n d s t y l e , a x e s , a x i s a n d
g r i d p r o p e r t i e s , t e x t a n d f o n t p r o p e r t i e s a n d s o o n . W h i l e m a t p l o t l i b d e f a u l t s
a r e r a t h e r g o o d i n m o s t c a s e s , y o u m a y w a n t t o m o d i f y s o m e p r o p e r t i e s f o r
s p e c i f i c c a s e s .
I n s t a n t i a t i n g d e f a u l t s
D o c u m e n t a t i o n
● C u s t o m i z i n g m a t p l o t l i b
I n t h e s c r i p t b e l o w , w e ' v e i n s t a n t i a t e d ( a n d c o m m e n t e d ) a l l t h e f i g u r e s e t t i n g s
t h a t i n f l u e n c e t h e a p p e a r a n c e o f t h e p l o t . T h e s e t t i n g s h a v e b e e n e x p l i c i t l y
s e t t o t h e i r d e f a u l t v a l u e s , b u t n o w y o u c a n i n t e r a c t i v e l y p l a y w i t h t h e v a l u e s
t o e x p l o r e t h e i r a f f e c t ( s e e L i n e p r o p e r t i e s a n d L i n e s t y l e s b e l o w ) .
C h a n g i n g c o l o r s a n d l i n e w i d t h s
D o c u m e n t a t i o n
● C o n t r o l l i n g l i n e p r o p e r t i e s
● L i n e A P I
A s a f i r s t s t e p , w e w a n t t o h a v e t h e c o s i n e i n b l u e a n d t h e s i n e i n r e d a n d a
s l i g h t l y t h i c k e r l i n e f o r b o t h o f t h e m . W e ' l l a l s o s l i g h t l y a l t e r t h e f i g u r e
s i z e t o m a k e i t m o r e h o r i z o n t a l .
...
plt.figure(figsize=(10,6), dpi=80)
plt.plot(X, C, color="blue", linewidth=2.5, linestyle="-")
plt.plot(X, S, color="red", linewidth=2.5, linestyle="-")
...
S e t t i n g l i m i t s
D o c u m e n t a t i o n
● x l i m ( ) c o m m a n d
● y l i m ( ) c o m m a n d
C u r r e n t l i m i t s o f t h e f i g u r e a r e a b i t t o o t i g h t a n d w e w a n t t o m a k e s o m e s p a c e
i n o r d e r t o c l e a r l y s e e a l l d a t a p o i n t s .
...
plt.xlim(X.min()*1.1, X.max()*1.1)
plt.ylim(C.min()*1.1, C.max()*1.1)
...
S e t t i n g t i c k s
D o c u m e n t a t i o n
● x t i c k s ( ) c o m m a n d
● y t i c k s ( ) c o m m a n d
● T i c k c o n t a i n e r
● T i c k l o c a t i n g a n d f o r m a t t i n g
C u r r e n t t i c k s a r e n o t i d e a l b e c a u s e t h e y d o n o t s h o w t h e i n t e r e s t i n g v a l u e s
( + / - π , + / - π / 2 ) f o r s i n e a n d c o s i n e . W e ' l l c h a n g e t h e m s u c h t h a t t h e y s h o w o n l y
t h e s e v a l u e s .
...
plt.xticks( [-np.pi, -np.pi/2, 0, np.pi/2, np.pi])
plt.yticks([-1, 0, +1])
...
S e t t i n g t i c k l a b e l s
D o c u m e n t a t i o n
● W o r k i n g w i t h t e x t
● x t i c k s ( ) c o m m a n d
● y t i c k s ( ) c o m m a n d
● s e t _ x t i c k l a b e l s ( )
● s e t _ y t i c k l a b e l s ( )
T i c k s a r e n o w p r o p e r l y p l a c e d b u t t h e i r l a b e l i s n o t v e r y e x p l i c i t . W e c o u l d
g u e s s t h a t 3 . 1 4 2 i s π b u t i t w o u l d b e b e t t e r t o m a k e i t e x p l i c i t . W h e n w e s e t
t i c k v a l u e s , w e c a n a l s o p r o v i d e a c o r r e s p o n d i n g l a b e l i n t h e s e c o n d a r g u m e n t
l i s t . N o t e t h a t w e ' l l u s e l a t e x t o a l l o w f o r n i c e r e n d e r i n g o f t h e l a b e l .
...
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],
[r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$'])
plt.yticks([-1, 0, +1],
[r'$-1$', r'$0$', r'$+1$'])
...
M o v i n g s p i n e s
D o c u m e n t a t i o n
● S p i n e s
● A x i s c o n t a i n e r
● T r a n s f o r m a t i o n s t u t o r i a l
S p i n e s a r e t h e l i n e s c o n n e c t i n g t h e a x i s t i c k m a r k s a n d n o t i n g t h e b o u n d a r i e s
o f t h e d a t a a r e a . T h e y c a n b e p l a c e d a t a r b i t r a r y p o s i t i o n s a n d u n t i l n o w , t h e y
w e r e o n t h e b o r d e r o f t h e a x i s . W e ' l l c h a n g e t h a t s i n c e w e w a n t t o h a v e t h e m i n
t h e m i d d l e . S i n c e t h e r e a r e f o u r o f t h e m ( t o p / b o t t o m / l e f t / r i g h t ) , w e ' l l d i s c a r d
t h e t o p a n d r i g h t b y s e t t i n g t h e i r c o l o r t o n o n e a n d w e ' l l m o v e t h e b o t t o m a n d
l e f t o n e s t o c o o r d i n a t e 0 i n d a t a s p a c e c o o r d i n a t e s .
...
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))
...
A d d i n g a l e g e n d
D o c u m e n t a t i o n
● L e g e n d g u i d e
● l e g e n d ( ) c o m m a n d
● L e g e n d A P I
L e t ' s a d d a l e g e n d i n t h e u p p e r l e f t c o r n e r . T h i s o n l y r e q u i r e s a d d i n g t h e
k e y w o r d a r g u m e n t l a b e l ( t h a t w i l l b e u s e d i n t h e l e g e n d b o x ) t o t h e p l o t
c o m m a n d s .
...
plt.plot(X, C, color="blue", linewidth=2.5, linestyle="-", label="cosine")
plt.plot(X, S, color="red", linewidth=2.5, linestyle="-", label="sine")
plt.legend(loc='upper left', frameon=False)
...
A n n o t a t e s o m e p o i n t s
D o c u m e n t a t i o n
● A n n o t a t i n g a x i s
● a n n o t a t e ( ) c o m m a n d
L e t ' s a n n o t a t e s o m e i n t e r e s t i n g p o i n t s u s i n g t h e a n n o t a t e c o m m a n d . W e c h o o s e t h e
2 π / 3 v a l u e a n d w e w a n t t o a n n o t a t e b o t h t h e s i n e a n d t h e c o s i n e . W e ' l l f i r s t
d r a w a m a r k e r o n t h e c u r v e a s w e l l a s a s t r a i g h t d o t t e d l i n e . T h e n , w e ' l l u s e
t h e a n n o t a t e c o m m a n d t o d i s p l a y s o m e t e x t w i t h a n a r r o w .
...
t = 2*np.pi/3
plt.plot([t,t],[0,np.cos(t )], color ='blue', linewidth=1.5, linestyle="--")
plt.scatter([t,],[np.cos(t ),], 50, color ='blue')
plt.annotate(r'$\sin(\frac{2\pi}{3})=\frac{\sqrt{3}}{2}$',
xy=(t, np.sin(t )), xycoords='data',
xytext=(+10, +30), textcoords='offset points', fontsize=16,
arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
plt.plot([t,t],[0,np.sin(t )], color ='red', linewidth=1.5, linestyle="--")
plt.scatter([t,],[np.sin(t ),], 50, color ='red')
plt.annotate(r'$\cos(\frac{2\pi}{3})=-\frac{1}{2}$',
xy=(t, np.cos(t )), xycoords='data',
xytext=(-90, -50), textcoords='offset points', fontsize=16,
arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
...
D e v i l i s i n t h e d e t a i l s
D o c u m e n t a t i o n
● A r t i s t s
● B B o x
T h e t i c k l a b e l s a r e n o w h a r d l y v i s i b l e b e c a u s e o f t h e b l u e a n d r e d l i n e s . W e c a n
m a k e t h e m b i g g e r a n d w e c a n a l s o a d j u s t t h e i r p r o p e r t i e s s u c h t h a t t h e y ' l l b e
r e n d e r e d o n a s e m i - t r a n s p a r e n t w h i t e b a c k g r o u n d . T h i s w i l l a l l o w u s t o s e e b o t h
t h e d a t a a n d t h e l a b e l s .
...
for label in ax.get_xticklabels() + ax.get_yticklabels():
label.set_fontsize(16 )
label.set_bbox(dict(facecolor='white', edgecolor='None', alpha=0.65 ))
...
S o f a r w e h a v e u s e d i m p l i c i t f i g u r e a n d a x e s c r e a t i o n . T h i s i s h a n d y f o r f a s t
p l o t s . W e c a n h a v e m o r e c o n t r o l o v e r t h e d i s p l a y u s i n g f i g u r e , s u b p l o t , a n d
a x e s e x p l i c i t l y . A f i g u r e i n m a t p l o t l i b m e a n s t h e w h o l e w i n d o w i n t h e u s e r
i n t e r f a c e . W i t h i n t h i s f i g u r e t h e r e c a n b e s u b p l o t s . W h i l e s u b p l o t p o s i t i o n s
t h e p l o t s i n a r e g u l a r g r i d , a x e s a l l o w s f r e e p l a c e m e n t w i t h i n t h e f i g u r e . B o t h
c a n b e u s e f u l d e p e n d i n g o n y o u r i n t e n t i o n . W e ' v e a l r e a d y w o r k e d w i t h f i g u r e s
a n d s u b p l o t s w i t h o u t e x p l i c i t l y c a l l i n g t h e m . W h e n w e c a l l p l o t , m a t p l o t l i b
c a l l s g c a ( ) t o g e t t h e c u r r e n t a x e s a n d g c a i n t u r n c a l l s g c f ( ) t o g e t t h e
c u r r e n t f i g u r e . I f t h e r e i s n o n e i t c a l l s f i g u r e ( ) t o m a k e o n e , s t r i c t l y
s p e a k i n g , t o m a k e a s u b p l o t ( 1 1 1 ) . L e t ' s l o o k a t t h e d e t a i l s .
F i g u r e s
A f i g u r e i s t h e w i n d o w s i n t h e G U I t h a t h a s " F i g u r e # " a s t i t l e . F i g u r e s
a r e n u m b e r e d s t a r t i n g f r o m 1 a s o p p o s e d t o t h e n o r m a l P y t h o n w a y s t a r t i n g
f r o m 0 . T h i s i s c l e a r l y M A T L A B - s t y l e . T h e r e a r e s e v e r a l p a r a m e t e r s t h a t
d e t e r m i n e w h a t t h e f i g u r e l o o k s l i k e :
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
frameon
True
draw figure frame or not
T h e d e f a u l t s c a n b e s p e c i f i e d i n t h e r e s o u r c e f i l e a n d w i l l b e u s e d m o s t o f t h e
t i m e . O n l y t h e n u m b e r o f t h e f i g u r e i s f r e q u e n t l y c h a n g e d .
W h e n y o u w o r k w i t h t h e G U I y o u c a n c l o s e a f i g u r e b y c l i c k i n g o n t h e x i n t h e
u p p e r r i g h t c o r n e r . Y o u c a n a l s o c l o s e a f i g u r e p r o g r a m m a t i c a l l y b y c a l l i n g
c l o s e . D e p e n d i n g o n t h e a r g u m e n t i t c l o s e s ( 1 ) t h e c u r r e n t f i g u r e ( n o
a r g u m e n t ) , ( 2 ) a s p e c i f i c f i g u r e ( f i g u r e n u m b e r o r f i g u r e i n s t a n c e a s
a r g u m e n t ) , o r ( 3 ) a l l f i g u r e s ( a l l a s a r g u m e n t ) .
A s w i t h o t h e r o b j e c t s , y o u c a n s e t f i g u r e p r o p e r t i e s w i t h t h e s e t _ s o m e t h i n g m e t h o d s .
S u b p l o t s
W i t h s u b p l o t y o u c a n a r r a n g e p l o t s i n a r e g u l a r g r i d . Y o u n e e d t o s p e c i f y t h e
n u m b e r o f r o w s a n d c o l u m n s a n d t h e n u m b e r o f t h e p l o t . N o t e t h a t t h e g r i d s p e c c o m m a n d i s a m o r e
p o w e r f u l a l t e r n a t i v e .
A x e s
A x e s a r e v e r y s i m i l a r t o s u b p l o t s b u t a l l o w p l a c e m e n t o f p l o t s a t a n y l o c a t i o n
i n t h e f i g u r e . S o i f w e w a n t t o p u t a s m a l l e r p l o t i n s i d e a b i g g e r o n e w e d o
s o w i t h a x e s .
T i c k s
W e l l f o r m a t t e d t i c k s a r e a n i m p o r t a n t p a r t o f p u b l i s h i n g - r e a d y
f i g u r e s . M a t p l o t l i b p r o v i d e s a t o t a l l y c o n f i g u r a b l e s y s t e m f o r t i c k s . T h e r e a r e
t i c k l o c a t o r s t o s p e c i f y w h e r e t i c k s s h o u l d a p p e a r a n d t i c k f o r m a t t e r s t o g i v e
t i c k s t h e a p p e a r a n c e y o u w a n t . M a j o r a n d m i n o r t i c k s c a n b e l o c a t e d a n d
f o r m a t t e d i n d e p e n d e n t l y f r o m e a c h o t h e r . B y d e f a u l t m i n o r t i c k s a r e n o t s h o w n ,
i . e . t h e r e i s o n l y a n e m p t y l i s t f o r t h e m b e c a u s e i t i s a s N u l l L o c a t o r ( s e e
b e l o w ) .
T i c k L o c a t o r s
T h e r e a r e s e v e r a l l o c a t o r s f o r d i f f e r e n t k i n d o f r e q u i r e m e n t s :
Class
Description
NullLocator
No ticks.
IndexLocator
Place a tick on every multiple of some base number of points plotted.
FixedLocator
Tick locations are fixed.
LinearLocator
Determine the tick locations.
MultipleLocator
Set a tick on every integer that is multiple of some base.
AutoLocator
Select no more than n intervals at nice locations.
LogLocator
Determine the tick locations for log axes.
A l l o f t h e s e l o c a t o r s d e r i v e f r o m t h e b a s e c l a s s m a t p l o t l i b . t i c k e r . L o c a t o r .
Y o u c a n m a k e y o u r o w n l o c a t o r d e r i v i n g f r o m i t . H a n d l i n g d a t e s a s t i c k s c a n b e
e s p e c i a l l y t r i c k y . T h e r e f o r e , m a t p l o t l i b p r o v i d e s s p e c i a l l o c a t o r s i n
m a t p l o t l i b . d a t e s .
F o r q u i t e a l o n g t i m e , a n i m a t i o n i n m a t p l o t l i b w a s n o t a n e a s y t a s k a n d w a s
d o n e m a i n l y t h r o u g h c l e v e r h a c k s . H o w e v e r , t h i n g s h a v e s t a r t e d t o c h a n g e s i n c e
v e r s i o n 1 . 1 a n d t h e i n t r o d u c t i o n o f t o o l s f o r c r e a t i n g a n i m a t i o n v e r y
i n t u i t i v e l y , w i t h t h e p o s s i b i l i t y t o s a v e t h e m i n a l l k i n d o f f o r m a t s ( b u t d o n ' t
e x p e c t t o b e a b l e t o r u n v e r y c o m p l e x a n i m a t i o n s a t 6 0 f p s t h o u g h ) .
D o c u m e n t a t i o n
● S e e A n i m a t i o n
T h e m o s t e a s y w a y t o m a k e a n a n i m a t i o n i n m a t p l o t l i b i s t o d e c l a r e a
F u n c A n i m a t i o n o b j e c t t h a t s p e c i f i e s t o m a t p l o t l i b w h a t i s t h e f i g u r e t o
u p d a t e , w h a t i s t h e u p d a t e f u n c t i o n a n d w h a t i s t h e d e l a y b e t w e e n f r a m e s .
D r i p d r o p
A v e r y s i m p l e r a i n e f f e c t c a n b e o b t a i n e d b y h a v i n g s m a l l g r o w i n g r i n g s
r a n d o m l y p o s i t i o n e d o v e r a f i g u r e . O f c o u r s e , t h e y w o n ' t g r o w f o r e v e r s i n c e t h e
w a v e i s s u p p o s e d t o d a m p w i t h t i m e . T o s i m u l a t e t h a t , w e c a n u s e a m o r e a n d
m o r e t r a n s p a r e n t c o l o r a s t h e r i n g i s g r o w i n g , u p t o t h e p o i n t w h e r e i t i s n o
m o r e v i s i b l e . A t t h i s p o i n t , w e r e m o v e t h e r i n g a n d c r e a t e a n e w o n e .
F i r s t s t e p i s t o c r e a t e a b l a n k f i g u r e :
# New figure with white background
fig = plt .figure (figsize = (6 ,6 ), facecolor = 'white' )
# New axis over the whole figure, no frame and a 1:1 aspect ratio
ax = fig .add_axes ([0 ,0 ,1 ,1 ], frameon = False , aspect = 1 )
N e x t , w e n e e d t o c r e a t e s e v e r a l r i n g s . F o r t h i s , w e c a n u s e t h e s c a t t e r p l o t
o b j e c t t h a t i s g e n e r a l l y u s e d t o v i s u a l i z e p o i n t s c l o u d , b u t w e c a n a l s o u s e i t
t o d r a w r i n g s b y s p e c i f y i n g w e d o n ' t h a v e a f a c e c o l o r . W e a l s o h a v e t o t a k e
c a r e o f i n i t i a l s i z e a n d c o l o r f o r e a c h r i n g s u c h t h a t w e h a v e a l l s i z e s b e t w e e n
a m i n i m u m a n d a m a x i m u m s i z e . I n a d d i t i o n , w e n e e d t o m a k e s u r e t h e l a r g e s t r i n g
i s a l m o s t t r a n s p a r e n t .
# Number of ring
n = 50
size_min = 50
size_max = 50 * 50
# Ring position
P = np .random .uniform (0 ,1 ,(n ,2 ))
# Ring colors
C = np .ones ((n ,4 )) * (0 ,0 ,0 ,1 )
# Alpha color channel goes from 0 (transparent) to 1 (opaque)
C [:,3 ] = np .linspace (0 ,1 ,n )
# Ring sizes
S = np .linspace (size_min , size_max , n )
# Scatter plot
scat = ax .scatter (P [:,0 ], P [:,1 ], s = S , lw = 0.5 ,
edgecolors = C , facecolors = 'None' )
# Ensure limits are [0,1] and remove ticks
ax .set_xlim (0 ,1 ), ax .set_xticks ([])
ax .set_ylim (0 ,1 ), ax .set_yticks ([])
N o w , w e n e e d t o w r i t e t h e u p d a t e f u n c t i o n f o r o u r a n i m a t i o n . W e k n o w t h a t a t
e a c h t i m e s t e p e a c h r i n g s h o u l d g r o w a n d b e c o m e m o r e t r a n s p a r e n t w h i l e t h e
l a r g e s t r i n g s h o u l d b e t o t a l l y t r a n s p a r e n t a n d t h u s r e m o v e d . O f c o u r s e , w e w o n ' t
a c t u a l l y r e m o v e t h e l a r g e s t r i n g b u t r e - u s e i t t o s e t a n e w r i n g a t a n e w r a n d o m
p o s i t i o n , w i t h n o m i n a l s i z e a n d c o l o r . H e n c e , w e k e e p t h e n u m b e r o f r i n g s
c o n s t a n t .
def update (frame ):
global P , C , S
# Every ring is made more transparent
C [:,3 ] = np .maximum (0 , C [:,3 ] - 1.0 / n )
# Each ring is made larger
S += (size_max - size_min ) / n
# Reset ring specific ring (relative to frame number)
i = frame % 50
P [i ] = np .random .uniform (0 ,1 ,2 )
S [i ] = size_min
C [i ,3 ] = 1
# Update scatter object
scat .set_edgecolors (C )
scat .set_sizes (S )
scat .set_offsets (P )
# Return the modified object
return scat ,
L a s t s t e p i s t o t e l l m a t p l o t l i b t o u s e t h i s f u n c t i o n a s a n u p d a t e f u n c t i o n f o r
t h e a n i m a t i o n a n d d i s p l a y t h e r e s u l t o r s a v e i t a s a m o v i e :
animation = FuncAnimation (fig , update , interval = 10 , blit = True , frames = 200 )
# animation.save('rain.gif', writer='imagemagick', fps=30, dpi=40)
plt .show ()
E a r t h q u a k e s
W e ' l l n o w u s e t h e r a i n a n i m a t i o n t o v i s u a l i z e e a r t h q u a k e s o n t h e p l a n e t f r o m
t h e l a s t 3 0 d a y s . T h e U S G S E a r t h q u a k e H a z a r d s P r o g r a m i s p a r t o f t h e N a t i o n a l
E a r t h q u a k e H a z a r d s R e d u c t i o n P r o g r a m ( N E H R P ) a n d p r o v i d e s s e v e r a l d a t a o n t h e i r
w e b s i t e . T h o s e d a t a a r e s o r t e d a c c o r d i n g t o
e a r t h q u a k e s m a g n i t u d e , r a n g i n g f r o m s i g n i f i c a n t o n l y d o w n t o a l l e a r t h q u a k e s ,
m a j o r o r m i n o r . Y o u w o u l d b e s u r p r i s e d b y t h e n u m b e r o f m i n o r e a r t h q u a k e s
h a p p e n i n g e v e r y h o u r o n t h e p l a n e t . S i n c e t h i s w o u l d r e p r e s e n t t o o m u c h d a t a
f o r u s , w e ' l l s t i c k t o e a r t h q u a k e s w i t h m a g n i t u d e > 4 . 5 . A t t h e t i m e o f w r i t i n g ,
t h i s a l r e a d y r e p r e s e n t m o r e t h a n 3 0 0 e a r t h q u a k e s i n t h e l a s t 3 0 d a y s .
F i r s t s t e p i s t o r e a d a n d c o n v e r t d a t a . W e ' l l u s e t h e u r l l i b l i b r a r y t h a t
a l l o w s u s t o o p e n a n d r e a d r e m o t e d a t a . D a t a o n t h e w e b s i t e u s e t h e C S V f o r m a t
w h o s e c o n t e n t i s g i v e n b y t h e f i r s t l i n e :
time,latitude,longitude,depth,mag,magType,nst,gap,dmin,rms,net,id,updated,place,type
2015-08-17T13:49:17.320Z,37.8365,-122.2321667,4.82,4.01,mw,...
2015-08-15T07:47:06.640Z,-10.9045,163.8766,6.35,6.6,mwp,...
W e a r e o n l y i n t e r e s t e d i n l a t i t u d e , l o n g i t u d e a n d m a g n i t u d e a n d w e w o n ' t p a r s e
t i m e o f e v e n t ( o k , t h a t ' s b a d , f e e l f r e e t o s e n d m e a P R ) .
import urllib
from mpl_toolkits .basemap import Basemap
# -> http://earthquake.usgs.gov/earthquakes/feed/v1.0/csv.php
feed = "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/"
# Significant earthquakes in the last 30 days
# url = urllib.request.urlopen(feed + "significant_month.csv")
# Magnitude > 4.5
url = urllib .request .urlopen (feed + "4.5_month.csv" )
# Magnitude > 2.5
# url = urllib.request.urlopen(feed + "2.5_month.csv")
# Magnitude > 1.0
# url = urllib.request.urlopen(feed + "1.0_month.csv")
# Reading and storage of data
data = url .read ()
data = data .split (b'\n ' )[+ 1 :- 1 ]
E = np .zeros (len (data ), dtype = [('position' , float , 2 ),
('magnitude' , float , 1 )])
for i in range (len (data )):
row = data [i ].split (',' )
E ['position' ][i ] = float (row [2 ]),float (row [1 ])
E ['magnitude' ][i ] = float (row [4 ])
N o w , w e n e e d t o d r a w t h e e a r t h o n a f i g u r e t o s h o w p r e c i s e l y w h e r e t h e e a r t h q u a k e
c e n t e r i s a n d t o t r a n s l a t e l a t i t u d e / l o n g i t u d e i n s o m e c o o r d i n a t e s m a t p l o t l i b
c a n h a n d l e . F o r t u n a t e l y , t h e r e i s t h e b a s e m a p p r o j e c t ( t h a t t e n d s t o b e r e p l a c e d b y t h e
m o r e c o m p l e t e c a r t o p y ) t h a t i s r e a l l y
s i m p l e t o i n s t a l l a n d t o u s e . F i r s t s t e p i s t o d e f i n e a p r o j e c t i o n t o d r a w t h e
e a r t h o n t o a s c r e e n ( t h e r e e x i s t s m a n y d i f f e r e n t p r o j e c t i o n s ) a n d w e ' l l s t i c k
t o t h e m i l l p r o j e c t i o n w h i c h i s r a t h e r s t a n d a r d f o r n o n - s p e c i a l i s t l i k e m e .
fig = plt .figure (figsize = (14 ,10 ))
ax = plt .subplot (1 ,1 ,1 )
earth = Basemap (projection = 'mill' )
N e x t , w e r e q u e s t t o d r a w c o a s t l i n e a n d f i l l c o n t i n e n t s :
earth .drawcoastlines (color = '0.50' , linewidth = 0.25 )
earth .fillcontinents (color = '0.95' )
T h e e a r t h o b j e c t w i l l a l s o b e u s e d t o t r a n s l a t e c o o r d i n a t e s q u i t e
a u t o m a t i c a l l y . W e a r e a l m o s t f i n i s h e d . L a s t s t e p i s t o a d a p t t h e r a i n c o d e a n d
p u t s o m e e y e c a n d y :
P = np .zeros (50 , dtype = [('position' , float , 2 ),
('size' , float , 1 ),
('growth' , float , 1 ),
('color' , float , 4 )])
scat = ax .scatter (P ['position' ][:,0 ], P ['position' ][:,1 ], P ['size' ], lw = 0.5 ,
edgecolors = P ['color' ], facecolors = 'None' , zorder = 10 )
def update (frame ):
current = frame % len (E )
i = frame % len (P )
P ['color' ][:,3 ] = np .maximum (0 , P ['color' ][:,3 ] - 1.0 / len (P ))
P ['size' ] += P ['growth' ]
magnitude = E ['magnitude' ][current ]
P ['position' ][i ] = earth (* E ['position' ][current ])
P ['size' ][i ] = 5
P ['growth' ][i ]= np .exp (magnitude ) * 0.1
if magnitude < 6 :
P ['color' ][i ] = 0 ,0 ,1 ,1
else :
P ['color' ][i ] = 1 ,0 ,0 ,1
scat .set_edgecolors (P ['color' ])
scat .set_facecolors (P ['color' ]* (1 ,1 ,1 ,0.25 ))
scat .set_sizes (P ['size' ])
scat .set_offsets (P ['position' ])
return scat ,
animation = FuncAnimation (fig , update , interval = 10 )
plt .show ()
I f e v e r y t h i n g w e n t w e l l , y o u s h o u l d o b t a i n s o m e t h i n g l i k e t h i s ( w i t h a n i m a t i o n ) :
R e g u l a r P l o t s
H i n t s
Y o u n e e d t o u s e t h e f i l l _ b e t w e e n
c o m m a n d .
S t a r t i n g f r o m t h e c o d e b e l o w , t r y t o r e p r o d u c e t h e g r a p h i c o n t h e r i g h t t a k i n g
c a r e o f f i l l e d a r e a s .
import numpy as np
import matplotlib.pyplot as plt
n = 256
X = np.linspace(-np.pi,np.pi,n,endpoint=True)
Y = np.sin(2*X)
plt.plot (X, Y+1, color='blue', alpha=1.00)
plt.plot (X, Y-1, color='blue', alpha=1.00)
plt.show()
C l i c k o n f i g u r e f o r s o l u t i o n .
S c a t t e r P l o t s
H i n t s
C o l o r i s g i v e n b y a n g l e o f ( X , Y ) .
S t a r t i n g f r o m t h e c o d e b e l o w , t r y t o r e p r o d u c e t h e g r a p h i c o n t h e r i g h t t a k i n g
c a r e o f m a r k e r s i z e , c o l o r a n d t r a n s p a r e n c y .
import numpy as np
import matplotlib.pyplot as plt
n = 1024
X = np.random.normal(0,1,n)
Y = np.random.normal(0,1,n)
plt.scatter(X,Y)
plt.show()
C l i c k o n f i g u r e f o r s o l u t i o n .
B a r P l o t s
H i n t s
Y o u n e e d t o t a k e c a r e o f t e x t a l i g n m e n t .
S t a r t i n g f r o m t h e c o d e b e l o w , t r y t o r e p r o d u c e t h e g r a p h i c o n t h e r i g h t b y
a d d i n g l a b e l s f o r r e d b a r s .
import numpy as np
import matplotlib.pyplot as plt
n = 12
X = np.arange(n )
Y1 = (1-X/float(n )) * np.random.uniform(0.5,1.0,n)
Y2 = (1-X/float(n )) * np.random.uniform(0.5,1.0,n)
plt.bar(X, +Y1, facecolor='#9999ff', edgecolor='white')
plt.bar(X, -Y2, facecolor='#ff9999', edgecolor='white')
for x,y in zip(X,Y1):
plt.text(x+0.4, y+0.05, '%.2f' % y, ha='center', va= 'bottom')
plt.ylim(-1.25,+1.25)
plt.show()
C l i c k o n f i g u r e f o r s o l u t i o n .
C o n t o u r P l o t s
H i n t s
Y o u n e e d t o u s e t h e c l a b e l
c o m m a n d .
S t a r t i n g f r o m t h e c o d e b e l o w , t r y t o r e p r o d u c e t h e g r a p h i c o n t h e r i g h t t a k i n g
c a r e o f t h e c o l o r m a p ( s e e C o l o r m a p s b e l o w ) .
import numpy as np
import matplotlib.pyplot as plt
def f(x,y): return (1-x/2+x**5+y**3)*np.exp(-x**2-y**2)
n = 256
x = np.linspace(-3,3,n)
y = np.linspace(-3,3,n)
X,Y = np.meshgrid(x,y)
plt.contourf(X, Y, f(X,Y), 8, alpha=.75, cmap='jet')
C = plt.contour(X, Y, f(X,Y), 8, colors='black', linewidth=.5)
plt.show()
C l i c k o n f i g u r e f o r s o l u t i o n .
I m s h o w
H i n t s
Y o u n e e d t o t a k e c a r e o f t h e o r i g i n o f t h e i m a g e i n t h e i m s h o w c o m m a n d a n d
u s e a c o l o r b a r .
S t a r t i n g f r o m t h e c o d e b e l o w , t r y t o r e p r o d u c e t h e g r a p h i c o n t h e r i g h t t a k i n g
c a r e o f c o l o r m a p , i m a g e i n t e r p o l a t i o n a n d o r i g i n .
import numpy as np
import matplotlib.pyplot as plt
def f(x,y): return (1-x/2+x**5+y**3)*np.exp(-x**2-y**2)
n = 10
x = np.linspace(-3,3,4*n)
y = np.linspace(-3,3,3*n)
X,Y = np.meshgrid(x,y)
plt.imshow(f(X,Y))
plt.show()
C l i c k o n f i g u r e f o r s o l u t i o n .
P i e C h a r t s
H i n t s
Y o u n e e d t o m o d i f y Z .
S t a r t i n g f r o m t h e c o d e b e l o w , t r y t o r e p r o d u c e t h e g r a p h i c o n t h e r i g h t t a k i n g
c a r e o f c o l o r s a n d s l i c e s s i z e .
import numpy as np
import matplotlib.pyplot as plt
n = 20
Z = np.random.uniform(0,1,n)
plt.pie(Z )
plt.show()
C l i c k o n f i g u r e f o r s o l u t i o n .
Q u i v e r P l o t s
H i n t s
Y o u n e e d t o d r a w a r r o w s t w i c e .
S t a r t i n g f r o m t h e c o d e a b o v e , t r y t o r e p r o d u c e t h e g r a p h i c o n t h e r i g h t t a k i n g
c a r e o f c o l o r s a n d o r i e n t a t i o n s .
import numpy as np
import matplotlib.pyplot as plt
n = 8
X,Y = np.mgrid[0:n,0:n]
plt.quiver(X,Y)
plt.show()
C l i c k o n f i g u r e f o r s o l u t i o n .
G r i d s
S t a r t i n g f r o m t h e c o d e b e l o w , t r y t o r e p r o d u c e t h e g r a p h i c o n t h e r i g h t t a k i n g
c a r e o f l i n e s t y l e s .
import numpy as np
import matplotlib.pyplot as plt
axes = gca()
axes.set_xlim(0,4)
axes.set_ylim(0,3)
axes.set_xticklabels([])
axes.set_yticklabels([])
plt.show()
C l i c k o n f i g u r e f o r s o l u t i o n .
M u l t i P l o t s
H i n t s
Y o u c a n u s e s e v e r a l s u b p l o t s w i t h d i f f e r e n t p a r t i t i o n .
S t a r t i n g f r o m t h e c o d e b e l o w , t r y t o r e p r o d u c e t h e g r a p h i c o n t h e r i g h t .
import numpy as np
import matplotlib.pyplot as plt
plt.subplot(2,2,1)
plt.subplot(2,2,3)
plt.subplot(2,2,4)
plt.show()
C l i c k o n f i g u r e f o r s o l u t i o n .
P o l a r A x i s
H i n t s
Y o u o n l y n e e d t o m o d i f y t h e a x e s l i n e .
S t a r t i n g f r o m t h e c o d e b e l o w , t r y t o r e p r o d u c e t h e g r a p h i c o n t h e r i g h t .
import numpy as np
import matplotlib.pyplot as plt
plt.axes([0,0,1,1])
N = 20
theta = np.arange(0.0, 2*np.pi, 2*np.pi/N)
radii = 10*np.random.rand(N )
width = np.pi/4*np.random.rand(N )
bars = plt.bar(theta, radii, width=width, bottom=0.0)
for r,bar in zip(radii, bars):
bar.set_facecolor( cm.jet(r/10.))
bar.set_alpha(0.5)
plt.show()
C l i c k o n f i g u r e f o r s o l u t i o n .
3 D P l o t s
H i n t s
Y o u n e e d t o u s e c o n t o u r f .
S t a r t i n g f r o m t h e c o d e b e l o w , t r y t o r e p r o d u c e t h e g r a p h i c o n t h e r i g h t .
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)
X = np.arange(-4, 4, 0.25)
Y = np.arange(-4, 4, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R )
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='hot')
plt.show()
C l i c k o n f i g u r e f o r s o l u t i o n .
T e x t
H i n t s
H a v e a l o o k a t t h e m a t p l o t l i b l o g o .
T r y t o d o t h e s a m e f r o m s c r a t c h !
C l i c k o n f i g u r e f o r s o l u t i o n .
M a t p l o t l i b b e n e f i t s f r o m e x t e n s i v e d o c u m e n t a t i o n a s w e l l a s a l a r g e
c o m m u n i t y o f u s e r s a n d d e v e l o p p e r s . H e r e a r e s o m e l i n k s o f i n t e r e s t :
T u t o r i a l s
● P y p l o t t u t o r i a l
● I n t r o d u c t i o n
● C o n t r o l l i n g l i n e p r o p e r t i e s
● W o r k i n g w i t h m u l t i p l e f i g u r e s a n d a x e s
● W o r k i n g w i t h t e x t
● I m a g e t u t o r i a l
● S t a r t u p c o m m a n d s
● I m p o r t i n g i m a g e d a t a i n t o N u m p y a r r a y s
● P l o t t i n g n u m p y a r r a y s a s i m a g e s
● T e x t t u t o r i a l
● T e x t i n t r o d u c t i o n
● B a s i c t e x t c o m m a n d s
● T e x t p r o p e r t i e s a n d l a y o u t
● W r i t i n g m a t h e m a t i c a l e x p r e s s i o n s
● T e x t r e n d e r i n g W i t h L a T e X
● A n n o t a t i n g t e x t
● A r t i s t t u t o r i a l
● I n t r o d u c t i o n
● C u s t o m i z i n g y o u r o b j e c t s
● O b j e c t c o n t a i n e r s
● F i g u r e c o n t a i n e r
● A x e s c o n t a i n e r
● A x i s c o n t a i n e r s
● T i c k c o n t a i n e r s
● P a t h t u t o r i a l
● I n t r o d u c t i o n
● B é z i e r e x a m p l e
● C o m p o u n d p a t h s
● T r a n s f o r m s t u t o r i a l
● I n t r o d u c t i o n
● D a t a c o o r d i n a t e s
● A x e s c o o r d i n a t e s
● B l e n d e d t r a n s f o r m a t i o n s
● U s i n g o f f s e t t r a n s f o r m s t o c r e a t e a s h a d o w e f f e c t
● T h e t r a n s f o r m a t i o n p i p e l i n e
M a t p l o t l i b d o c u m e n t a t i o n
● U s e r g u i d e
● F A Q
● I n s t a l l a t i o n
● U s a g e
● H o w - T o
● T r o u b l e s h o o t i n g
● E n v i r o n m e n t V a r i a b l e s
● S c r e e n s h o t s
C o d e d o c u m e n t a t i o n
T h e c o d e i s f a i r l y w e l l d o c u m e n t e d a n d y o u c a n q u i c k l y a c c e s s a s p e c i f i c
c o m m a n d f r o m w i t h i n a p y t h o n s e s s i o n :
>>> import matplotlib.pyplot as plt
>>> help(plt)
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.
...
G a l l e r i e s
T h e m a t p l o t l i b g a l l e r y i s
a l s o i n c r e d i b l y u s e f u l w h e n y o u s e a r c h h o w t o r e n d e r a g i v e n g r a p h i c . E a c h
e x a m p l e c o m e s w i t h i t s s o u r c e .
A s m a l l e r g a l l e r y i s a l s o a v a i l a b l e h e r e .
M a i l i n g l i s t s
F i n a l l y , t h e r e i s a u s e r m a i l i n g l i s t w h e r e y o u c a n
a s k f o r h e l p a n d a d e v e l o p e r s m a i l i n g l i s t t h a t i s m o r e
t e c h n i c a l .
H e r e i s a s e t o f t a b l e s t h a t s h o w m a i n p r o p e r t i e s a n d s t y l e s .
L i n e p r o p e r t i e s
Property
Description
Appearance
alpha (or a)
alpha transparency on 0-1 scale
antialiased
True or False - use antialised rendering
color (or c)
matplotlib color arg
linestyle (or ls)
see Line properties
linewidth (or lw)
float, the line width in points
solid_capstyle
Cap style for solid lines
solid_joinstyle
Join style for solid lines
dash_capstyle
Cap style for dashes
dash_joinstyle
Join style for dashes
marker
see Markers
markeredgewidth (mew)
line width around the marker symbol
markeredgecolor (mec)
edge color if a marker is used
markerfacecolor (mfc)
face color if a marker is used
markersize (ms )
size of the marker in points
L i n e s t y l e s
Symbol
Description
Appearance
-
solid line
--
dashed line
-.
dash-dot line
:
dotted line
.
points
,
pixels
o
circle
^
triangle up
v
triangle down
<
triangle left
>
triangle right
s
square
+
plus
x
cross
D
diamond
d
thin diamond
1
tripod down
2
tripod up
3
tripod left
4
tripod right
h
hexagon
H
rotated hexagon
p
pentagon
|
vertical line
_
horizontal line
M a r k e r s
Symbol
Description
Appearance
0
tick left
1
tick right
2
tick up
3
tick down
4
caret left
5
caret right
6
caret up
7
caret down
o
circle
D
diamond
h
hexagon 1
H
hexagon 2
_
horizontal line
1
tripod down
2
tripod up
3
tripod left
4
tripod right
8
octagon
p
pentagon
^
triangle up
v
triangle down
<
triangle left
>
triangle right
d
thin diamond
,
pixel
+
plus
.
point
s
square
*
star
|
vertical line
x
cross
r'$\sqrt{2}$'
any latex expression
C o l o r m a p s
A l l c o l o r m a p s c a n b e r e v e r s e d b y a p p e n d i n g _ r . F o r i n s t a n c e , g r a y _ r i s
t h e r e v e r s e o f g r a y .
I f y o u w a n t t o k n o w m o r e a b o u t c o l o r m a p s , s e e D o c u m e n t i n g t h e m a t p l o t l i b
c o l o r m a p s .
B a s e
Name
Appearance
autumn
bone
cool
copper
flag
gray
hot
hsv
jet
pink
prism
spectral
spring
summer
winter
G I S T
Name
Appearance
gist_earth
gist_gray
gist_heat
gist_ncar
gist_rainbow
gist_stern
gist_yarg
S e q u e n t i a l
Name
Appearance
BrBG
PiYG
PRGn
PuOr
RdBu
RdGy
RdYlBu
RdYlGn
Spectral
D i v e r g i n g
Name
Appearance
Blues
BuGn
BuPu
GnBu
Greens
Greys
Oranges
OrRd
PuBu
PuBuGn
PuRd
Purples
RdPu
Reds
YlGn
YlGnBu
YlOrBr
YlOrRd
Q u a l i t a t i v e
Name
Appearance
Accent
Dark2
Paired
Pastel1
Pastel2
Set1
Set2
Set3
M i s c e l l a n e o u s
Name
Appearance
afmhot
binary
brg
bwr
coolwarm
CMRmap
cubehelix
gnuplot
gnuplot2
ocean
rainbow
seismic
terrain
A b o u t
M a t p l o t l i b t u t o r i a l f o r b e g i n n e r
T o p i c s
p y t h o n
m a t p l o t l i b
t u t o r i a l
R e s o u r c e s
R e a d m e
V e r s i o n 1 . 0
L a t e s t
A u g 2 1 , 2 0 1 5
N o p a c k a g e s p u b l i s h e d
L a n g u a g e s
●
P y t h o n
8 2 . 8 %
●
C S S
1 5 . 9 %
●
M a k e f i l e
1 . 3 %
● © 2 0 2 0 G i t H u b , I n c .
● T e r m s
● P r i v a c y
● S e c u r i t y
● S t a t u s
● H e l p
● C o n t a c t G i t H u b
● P r i c i n g
● A P I
● T r a i n i n g
● B l o g
● A b o u t
Y o u c a n ’ t p e r f o r m t h a t a c t i o n a t t h i s t i m e .
Y o u s i g n e d i n w i t h a n o t h e r t a b o r w i n d o w . R e l o a d t o r e f r e s h y o u r s e s s i o n .
Y o u s i g n e d o u t i n a n o t h e r t a b o r w i n d o w . R e l o a d t o r e f r e s h y o u r s e s s i o n .