Matplotlib 簡介與使用

44
MATPLOTLIB 墋Օ VIC 2016/7/28

Transcript of Matplotlib 簡介與使用

Page 1: Matplotlib 簡介與使用

MATPLOTLIB VIC

2016/7/28

Page 2: Matplotlib 簡介與使用

OUTLINE

• Self introduction

• What is matplotlib?

• How to use matplotlib?

• Some examples

• Demo

Page 3: Matplotlib 簡介與使用

SELF INTRODUCTION

• Vic

• NRL

• Python web

• Twitter: https://twitter.com/vrootic

• GitHub: https://github.com/vrootic

• Email: [email protected]

3

Page 4: Matplotlib 簡介與使用

WHAT CAN IT DO?

Page 5: Matplotlib 簡介與使用

WHAT IS MATPLOTLIB?

• Matplotlib Python libMatlab Matplotlib cmd interfaceIPython script

5

Page 6: Matplotlib 簡介與使用

WHAT IS MATPLOTLIB?

• Python data MathematicaAdaptive Plotting

• Multiplot

• LaTex render

6

Page 7: Matplotlib 簡介與使用

WHAT IS MATPLOTLIB?

• (cmd & OO) API

7

Page 8: Matplotlib 簡介與使用

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

Page 9: Matplotlib 簡介與使用

HOW TO USE MATPLOTLIB?

• jupyter notebook

• python shell

9

Page 10: Matplotlib 簡介與使用

JUPYTER NOTEBOOK

• Add magic command

• %pylab inline(not recommeded)

• %matplotlib inline

10

Page 11: Matplotlib 簡介與使用

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

Page 12: Matplotlib 簡介與使用

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

Page 13: Matplotlib 簡介與使用

PRACTICE 1

Hint:pi = np.pix = np.linspace(-pi, pi, 256)sinx = np.sin(x)cosx = np.cos(x)

13

Page 14: Matplotlib 簡介與使用

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)

Page 15: Matplotlib 簡介與使用

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

Page 16: Matplotlib 簡介與使用

(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

Page 17: Matplotlib 簡介與使用

(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

Page 18: Matplotlib 簡介與使用

(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

Page 19: Matplotlib 簡介與使用

(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)

Page 20: Matplotlib 簡介與使用

PRACTICE 2

20

Page 21: Matplotlib 簡介與使用

import numpy as npimport matplotlib.pyplot as plt

%matplotlib inline

for idx, color in enumerate("rgbyck"): plt.subplot(3, 2, 1+idx, axisbg=color)

Page 22: Matplotlib 簡介與使用

BASIC EXAMPLES

http://matplotlib.org/users/gridspec.html

22

Page 23: Matplotlib 簡介與使用

PLOT STYLE CHEATSHEET

Ex: Draw blue star line: plot(x, y, ‘b*’)

23

Page 24: Matplotlib 簡介與使用

(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

Page 25: Matplotlib 簡介與使用

(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)

Page 26: Matplotlib 簡介與使用

(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

Page 27: Matplotlib 簡介與使用

(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

Page 28: Matplotlib 簡介與使用

(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

Page 29: Matplotlib 簡介與使用

GALLERY

SOME EXAMPLES

http://matplotlib.org/gallery.html

http://www.labri.fr/perso/nrougier/coding/gallery/

29

Page 30: Matplotlib 簡介與使用

LOCATION AWARE SENSING SYSTEM

LASS

• https://www.facebook.com/groups/1607718702812067/

• Hackpad

• https://lass.hackpad.com/LASS-README-DtZ5T6DXLbu

• http://lass-net.org

Page 31: Matplotlib 簡介與使用

LOCATION AWARE SENSING SYSTEM

LASS

LASS Bottom up

Page 32: Matplotlib 簡介與使用

PRACTICE 3

Data source: http://nrl.iis.sinica.edu.tw/LASS/last-all-airbox.json

32

Page 33: Matplotlib 簡介與使用

33

Page 34: Matplotlib 簡介與使用

Document

34

Page 35: Matplotlib 簡介與使用

jupyter

35

Page 36: Matplotlib 簡介與使用

shell

36

Page 37: Matplotlib 簡介與使用

EXAMPLES(SHELL)

import numpy as npimport matplotlib.pyplot as plt

X = np.linspace(0, 1, 256)

plt.plot(X)

plt.show() #

37

Page 38: Matplotlib 簡介與使用

38

Page 39: Matplotlib 簡介與使用

39

Page 40: Matplotlib 簡介與使用

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

Page 41: Matplotlib 簡介與使用

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()

Page 42: Matplotlib 簡介與使用

SUMMARY

• Matplotlib class Figure -> Axes -> (Line2D, Text, etc) Figure Axes( ) matplotlibAxes

• -> -> Matplotlib gallery

• jupyter matplotlib inlineenvironment

(ex. )

42

Page 43: Matplotlib 簡介與使用

DEMO

43

Page 44: Matplotlib 簡介與使用

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