View
710
Download
1
Category
Preview:
Citation preview
MATPLOTLIB VIC
2016/7/28
OUTLINE
• Self introduction
• What is matplotlib?
• How to use matplotlib?
• Some examples
• Demo
SELF INTRODUCTION
• Vic
• NRL
• Python web
•
• Twitter: https://twitter.com/vrootic
• GitHub: https://github.com/vrootic
• Email: vrootic@gmail.com
3
WHAT CAN IT DO?
WHAT IS MATPLOTLIB?
• Matplotlib Python libMatlab Matplotlib cmd interfaceIPython script
5
WHAT IS MATPLOTLIB?
•
• Python data MathematicaAdaptive Plotting
• Multiplot
• LaTex render
6
WHAT IS MATPLOTLIB?
•
• (cmd & OO) API
•
7
HOW TO USE MATPLOTLIB?
• Environment
• python==3.5
• numpy==1.11.0
• matplotlib==1.5.0
• jupyter notebook
• https://gist.github.com/vrootic/9c857c0987788774bf3d26682af3152e
8
HOW TO USE MATPLOTLIB?
• jupyter notebook
• python shell
9
JUPYTER NOTEBOOK
• Add magic command
• %pylab inline(not recommeded)
• %matplotlib inline
10
WARM UP(IN JUPYTER)
import matplotlib.pyplot as pltimport numpy as np
%matplotlib inline
# plot(X-axis, Y-axis, style=‘b-’)plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
11
WARM UP(IN JUPYTER)import matplotlib.pyplot as pltimport numpy as np
%matplotlib inline
c1 = [1, 2, 3, 4] # c2 = [1, 4, 9, 16] # plt.xlim(-1, 4) # xplt.ylim(0, 18) # yplt.plot(c1, 'b-')plt.plot(c2, 'ro')
12
PRACTICE 1
Hint:pi = np.pix = np.linspace(-pi, pi, 256)sinx = np.sin(x)cosx = np.cos(x)
13
import numpy as npimport matplotlib.pyplot as plt
%matplotlib inline
X = np.linspace(-np.pi, np.pi, 256)cosx = np.cos(X)sinx = np.sin(X)
plt.xlim(-4.0, 4.0)plt.ylim(-1.0, 1.0)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi])plt.yticks([-1, 0, 1])
plt.plot(X, cosx, 'b-', linewidth=1.0)plt.plot(X, sinx, 'r-', linewidth=1.0)
BASIC EXAMPLES(IN JUPYTER)X = np.linspace(-np.pi, np.pi, 256)cosx = np.cos(X)sinx = np.sin(X)
plt.xlim(-4.0, 4.0)plt.ylim(-1.0, 1.0)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi])plt.yticks([-1, 0, 1])
plt.plot(X, cosx, 'b-', linewidth=1.0)plt.plot(X, sinx, 'r-', linewidth=1.0)
15
(CHANGE AXIS DISPLAY)
BASIC EXAMPLES(IN JUPYTER)
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$'])
16
(ADD LEGEND)
BASIC EXAMPLES(IN JUPYTER)
plt.plot(X, cosx, 'b-', linewidth=1.0, label="cosine")plt.plot(X, sinx, 'r-', linewidth=1.0, label="sine")plt.legend(loc=‘upper left’)
17
(CHANGE SPINE)
BASIC EXAMPLES(IN JUPYTER)
ax = plt.gca() # spineax.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',np.pi/2))
18
(SUBPLOT)
BASIC EXAMPLES(IN JUPYTER)
plt.axes([.1, .1, 1, 1])plt.axes([.2, .2, .3, .3], axisbg='green')
plt.subplot(2, 1, 1, axisbg=‘y’)plt.subplot(2, 1, 2)
19
plt.subplot(2, 1, 1)
plt.subplot(2, 1, 2)
PRACTICE 2
20
import numpy as npimport matplotlib.pyplot as plt
%matplotlib inline
for idx, color in enumerate("rgbyck"): plt.subplot(3, 2, 1+idx, axisbg=color)
BASIC EXAMPLES
http://matplotlib.org/users/gridspec.html
22
PLOT STYLE CHEATSHEET
Ex: Draw blue star line: plot(x, y, ‘b*’)
23
(SCATTER)
SOME EXAMPLES
n = 1024X = np.random.normal(0, 1, n)Y = np.random.normal(0, 1, n)T = np.arctan2(Y,X)
plt.axes([0.025,0.025,0.95,0.95])plt.scatter(X,Y, s=75, c=T, alpha=.5)
plt.xlim(-1.5,1.5)plt.xticks([])plt.ylim(-1.5,1.5)plt.yticks([])
24
# matplotlib.colors.Colormap
(HISTOGRAM)
SOME EXAMPLES
25
n = 7X = np.arange(n)Y1 = [0.5, 0.6, 0.65, 0.7, 0.8, 0.55, 0.9]
plt.bar(X, Y1, facecolor='#9999ff')
for x,y in zip(X,Y1): if y == sorted(Y1)[1]: plt.text(x+0.4, y+0.05, '%.2f' % y, ha='center', va= 'bottom')
plt.ylim(0, 1.25)
(HISTOGRAM)
SOME EXAMPLES
n = 12X = 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)
26
(CONTOUR)
SOME EXAMPLES
def f(x,y): return (1-x/2+x**5+y**3)*np.exp(-x**2 - y**2)
n = 256x = 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)
27
(COLORMAP)
SOME EXAMPLES
def f(x,y): return (1-x/2+x**5+y**3)*np.exp(-x**2- y**2)
n = 10x = np.linspace(-3,3,3.5*n)y = np.linspace(-3,3,3.0*n)X,Y = np.meshgrid(x,y)Z = f(X,Y)
plt.axes([0.025,0.025,0.95,0.95])plt.imshow(Z,interpolation='nearest',
cmap='bone', origin='lower')plt.colorbar(shrink=.92)
plt.xticks([]), plt.yticks([])
28
GALLERY
SOME EXAMPLES
http://matplotlib.org/gallery.html
http://www.labri.fr/perso/nrougier/coding/gallery/
29
LOCATION AWARE SENSING SYSTEM
LASS
•
• https://www.facebook.com/groups/1607718702812067/
• Hackpad
• https://lass.hackpad.com/LASS-README-DtZ5T6DXLbu
•
• http://lass-net.org
LOCATION AWARE SENSING SYSTEM
LASS
•
•
LASS Bottom up
PRACTICE 3
Data source: http://nrl.iis.sinica.edu.tw/LASS/last-all-airbox.json
32
33
Document
34
jupyter
35
shell
36
EXAMPLES(SHELL)
import numpy as npimport matplotlib.pyplot as plt
X = np.linspace(0, 1, 256)
plt.plot(X)
plt.show() #
37
38
39
BASIC EXAMPLES(SHELL)
import numpy as npimport matplotlib.pyplot as plt
X = np.linspace(0, 1, 256)
fig1 = plt.figure("fig_1")ax1 = fig1.add_subplot(1, 1, 1)ax1.plot(X)
fig1.savefig(“fig_1.jpg")plt.show()
40
SHELL VS. JUPYTER
COMPARE
import numpy as npimport matplotlib.pyplot as plt
%matplotlib inline
X = np.linspace(0, 1, 256)
plt.plot(X)
import numpy as npimport matplotlib.pyplot as plt
X = np.linspace(0, 1, 256)
fig1 = plt.figure("fig_1")ax1 = fig1.add_subplot(1, 1, 1)ax1.plot(X)
fig1.savefig(“fig_1.jpg")plt.show()
SUMMARY
• Matplotlib class Figure -> Axes -> (Line2D, Text, etc) Figure Axes( ) matplotlibAxes
• -> -> Matplotlib gallery
• jupyter matplotlib inlineenvironment
•
(ex. )
42
DEMO
43
REFERENCE
• http://liam0205.me/2014/09/11/matplotlib-tutorial-zh-cn/
• http://www.cnblogs.com/wei-li/archive/2012/05/23/2506940.html
• http://darksair.org/wiki/matplotlib.html
• http://2484055.blog.51cto.com/2474055/1334257
• http://matplotlib.org/users/pyplot_tutorial.html
• http://matplotlib.org/users/style_sheets.html
• http://rickchungtw-blog.logdown.com/tags/Matplotlib
• http://yukuan.blogspot.tw/2006/12/analyze-sunspots.html
• http://stackoverflow.com/questions/12987624/confusion-between-numpy-scipy-matplotlib-and-pylab
• http://hhtucode.blogspot.tw/2013/04/ml-gradient-descent-algorithm.html
• http://matplotlib.org/examples/subplots_axes_and_figures/subplot_demo.html
• http://matplotlib.org/users/shell.html
44
Recommended