More Related Content Similar to Matplotlib 簡介與使用 (20) Matplotlib 簡介與使用3. SELF INTRODUCTION
• Vic
• NRL
• Python web
•
• Twitter: https://twitter.com/vrootic
• GitHub: https://github.com/vrootic
• Email: vrootic@gmail.com
3
8. 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
9. HOW TO USE MATPLOTLIB?
• jupyter notebook
• python shell
9
11. WARM UP(IN JUPYTER)
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
# plot(X-axis, Y-axis, style=‘b-’)
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
11
12. WARM UP(IN JUPYTER)
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
c1 = [1, 2, 3, 4] #
c2 = [1, 4, 9, 16] #
plt.xlim(-1, 4) # x
plt.ylim(0, 18) # y
plt.plot(c1, 'b-')
plt.plot(c2, 'ro')
12
14. import numpy as np
import 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)
15. 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
16. (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
17. (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
18. (CHANGE SPINE)
BASIC EXAMPLES(IN JUPYTER)
ax = plt.gca() # spine
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',np.pi/2))
18
19. (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)
21. import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
for idx, color in enumerate("rgbyck"):
plt.subplot(3, 2, 1+idx, axisbg=color)
24. (SCATTER)
SOME EXAMPLES
n = 1024
X = 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
25. (HISTOGRAM)
SOME EXAMPLES
25
n = 7
X = 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)
26. (HISTOGRAM)
SOME EXAMPLES
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)
26
27. (CONTOUR)
SOME EXAMPLES
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)
27
28. (COLORMAP)
SOME EXAMPLES
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,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
30. LOCATION AWARE SENSING SYSTEM
LASS
•
• https://www.facebook.com/groups/1607718702812067/
• Hackpad
• https://lass.hackpad.com/LASS-README-DtZ5T6DXLbu
•
• http://lass-net.org
40. BASIC EXAMPLES(SHELL)
import numpy as np
import 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
41. SHELL VS. JUPYTER
COMPARE
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
X = np.linspace(0, 1, 256)
plt.plot(X)
import numpy as np
import 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()
42. SUMMARY
• Matplotlib class Figure -> Axes -> (Line2D,
Text, etc) Figure Axes( ) matplotlib
Axes
• -> -> Matplotlib gallery
• jupyter matplotlib inline
environment
•
(ex. )
42
44. 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