matplotlib

16 downloads 87 Views 4MB Size Report
"Sandro Tosi: Matplotlib for Python Developers, Packt Publishing (2009)" gnuplot matlab. R excel mathematica ... matplotlib python gnuplot. EPS, PDF, PNG, PS, ...
matplotlib matplotlib matplotlib

matplotlib

"Sandro Tosi: Matplotlib for Python Developers, Packt Publishing (2009)"

gnuplot matlab R excel mathematica ...

matplotlib python

gnuplot

EPS, PDF, PNG, PS, SVG latex

1 2 3 4

import  matplotlib.pyplot  as  plt   plt.plot([3,  1,  5,  8,  7,  6,  4,  2]) plt.show() matplotlib

pyplot

”plt.

?



plt plt

[3, 1, 5, 8, 7, 6, 4, 2]

y

x

plot 0

y

x

y

x=range(10) y=  [xt**2  for  xt  in  x] plt.plot(x,  y) plt.show() for xt

g2 ?

[xt**2 for xt in x] x

x

0, 1, 2, ..

x 1 2 3 4

0, 1, 2, 3, 4, 5, 6, 7

xt**2

1 2 3 4 5 6

x=range(10) y=[] for  xt  in  x:        y.append(xt**2) plt.plot(x,  y) plt.show()

?

” ” range(10) 0

10-1=9 range(x, y, z) x

y-1

z

x range() numpy 1 2 3 4 5 6

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] x z

python arange()

import  matplotlib.pyplot  as  plt import  numpy  as  np   x=np.arange(0.5,  3.0,  0.2) plt.plot(x,  [3.0*xt**2-­‐xt**3  for  xt  in  x]) plt.show() range()

?

plot show

1 2 3 4 5 6 7

g4

import  matplotlib.pyplot  as  plt import  numpy  as  np   x=np.arange(0.0,  2.0,  0.1) plt.plot(x,  [xt**2  for  xt  in  x]) plt.plot(x,  [xt**3  for  xt  in  x]) plt.show()

?

2 plt.plot(x, [xt**2 for xt in x], x, [xt**3 for xt in x])

plt.plot(x, x**2, x, x**3) arange()

range np.arrange

numpy python

y=ax(x-a) a

0

x

20

0

2.5

plt.grid(True)

plt.axis(ymin= , xmin= ) plt.xlabel(" ") plt.ylabel(" x y plt.title("

") ")

plt.plot(x, y, label="

")

x, y plt.legend() plt.legend(["

", "

"])

ymin

0.1 1

g5

True python plt.axis([xmin, xmax, ymin, ymax]) x xmin xmax y

10

ymax

plt.legend(loc="upper left") upper, lower, left, right 1 2 3 4 5 6 7 8 9 10 11

import  matplotlib.pyplot  as  plt import  numpy  as  np   plt.plot([1,  2,  3,  4],  [1,  -­‐3,  5,  2],  label="line  1") plt.plot([1,  2,  3,  4],  [7,  6,  3,  -­‐1],  label="line  2") plt.axis([0,  5,  -­‐4,  8]) plt.xlabel("X-­‐axis") plt.ylabel("Y-­‐axis") plt.title("Title") plt.legend(loc="upper  right") plt.show()

plot()

x

?

y plt.plot(x, y,

"

")

plt.plot(x, y, 'y') y:yellow b:blue, c: cyan, g: green, k: black, m: magenta, r: red, w: white, y: yellow "#FF00FF" HTML "(1, 0, 1)" RGB "'(1, 0, 1, 1)" RGBA "0.6"

plt.plot(x, y, '--') "-":

"--":

"-.":

":":

plt.plot(x, y, '.') ".":

",":

"o":

"1": "4": "+":

"^"

"":

"2":

"3": "|":

"v":

"x":

"D":

"s": "d":

"p":

"h":

"H":

"-":

plt.plot(x1, y1, format1, x2, y2, format2, ...) "

" plot() plot()

g6

color / c linestyle linewidth marker markerdgecolor markeredgewidth markerfacecolor markersize

1 2 3 4 5 6 7 8

? import  matplotlib.pyplot  as  plt import  numpy  as  np   x=np.arange(1,  3,  0.1) plt.plot(x,  x,  'x',  x,  x+0.5,  'cx-­‐-­‐',  x,  x+1,  'mo:') plt.plot(x,  x+1.5,  'kh-­‐',  x,  x+2.0,  'gp-­‐-­‐',  linewidth=3,  markersize=10) plt.legend(['x',  'cx-­‐-­‐',  'mo:',  'kh-­‐',  'gp-­‐-­‐'],  loc="upper  left") plt.show()

plt.xtics( )

1 2 3 4 5 6 7 8 9

)

plt.xtics(

,

g7

import  matplotlib.pyplot  as  plt import  numpy  as  np   x=range(7) y=[2,  6,  4,  3,  5,  5,  7] plt.plot(y) plt.xticks(x,  ['Mon',  'Tue',  'Wed',  'Thu',  'Fri',  'Sat',  'Sun']) plt.yticks(range(1,  8,  2)) plt.show()

?

0

+y v[m/s] y

(

v_0[m/s] [m]

g=9.8[m/s^2])

t

y t 0 1

matplotlib

t[s]

3

y 0.1

v_0

10

20

plt.errorbar 1 2 3 4 5 6 7 8

import  matplotlib.pyplot  as  plt import  numpy  as  np   x=np.arange(0,  1,  0.1) y=x**2 e=np.abs(np.random.randn(len(y))) plt.errorbar(x,  y,  yerr=e) plt.show() ecolor elinewidth, capsize -y

x

?

yerr=[e1, e2]

+y

xerr

plt.bar 1 2 3 4 5 6 7

import  matplotlib.pyplot  as  plt import  numpy  as  np   x=np.arange(1,  6) y=np.exp(x)   plt.barh(x,  y)

?

8

plt.show()

x

np.bar np.barh

1 2

x=[45,  35,  20] labels=  ['Salt',  'Sauce',  'Soi']

?

3 4 5

plt.figure(figsize=(8,  8)) plt.pie(x,  labels=labels) plt.show()

3

1

1

2

1

np.array

1

2

h8b 1 2 3 4 5 6 7 8

import  matplotlib.pyplot  as  plt import  numpy  as  np import  pylab  as  pl   matr=  np.random.rand(21,  31) plt.pcolor(matr  ,  vmin=0,  vmax=1) plt.colorbar() plt.show()

np.random.rand(x, y) [0, 1) 6 pcolor

?

x*y

2

np.array vmin vmax ”cmap=plt.cm.gray” ”cmap=plt.cm.binary” 7

np.array 1 2 3 4 5 6

import  matplotlib.pyplot  as  plt import  numpy  as  np   y=np.random.randn(1000) plt.hist(y,  10) plt.show()

plt.hist(y, n)

y

n

np.random.randn(x) np.array

?

0 hist

1

x

1 2 3 4 5 6

import  matplotlib.pyplot  as  plt import  numpy  as  np x=  np.random.randn(1000) y=  np.random.randn(1000) plt.scatter(x,  y) plt.show()

?

plt.text(x, y, text) (x, y) 0

text 1

tex $

tex

plt.text(0.5

0.5, r"$\frac{1}{\alpha}$") r

1 2 3 4 5 6

import  matplotlib.pyplot  as  plt import  numpy  as  np   x=np.arange(0.5,  3.0,  0.2) plt.plot(x,  [3.0*xt**2-­‐xt**3  for  xt  in  x]) plt.show()

\

python

?

1 2 3 4 5 6 7 8 9 10

import  matplotlib.pyplot  as  plt import  numpy  as  np     fig=  plt.figure() ax=  fig.add_subplot(1,  1,  1)   x=np.arange(0.5,  3.0,  0.2) ax.plot(x,  [3.0*xt**2-­‐xt**3  for  xt  in  x]) plt.show()

?

3 fig fig a*b

add_subplot(a, b, c)

a, b, c

c ax

ax

axis

matplotlib

axis ax.set_xlabel(

1 2 3 4 5 6 7 8 9 10

ax ), ax.set_title(

)

import  matplotlib.pyplot  as  plt import  numpy  as  np   fig=  plt.figure() x=  np.arange(0,  1,  0.1) ax1=  fig.add_subplot(2,  1,  1) ax1.plot(x,  np.exp(x)) ax2=fig.add_subplot(212) ax2.plot(x,  x**5) plt.show()

fig

?

fig

add_subplot() ax1

add_subplot(a, b, c) 2x1 ax1 ax2

a

b

c

1 plt 2

plot

1 2 3 4 5 6 7 8 9 10 11 ax1

import  matplotlib.pyplot  as  plt import  numpy  as  np   fig=  plt.figure() x=  np.arange(0,  1,  0.1) ax1=  fig.add_subplot(1,  1,  1) ax1.plot(x,  np.exp(x)) fig2=  plt.figure() ax2=fig2.add_subplot(111) ax2.plot(x,  x**5) plt.show() fig1

ax2

fig2

?

python

sampledata.txt sampledata.txt 1 7 5 2 6 6 3 5 7 4 4 8 5 3 9

1 2 3 4 5 6 7 8 9

import  matplotlib.pyplot  as  plt import  numpy  as  np   import  os,  sys os.chdir(os.path.dirname(os.path.abspath(sys.argv[0])))   data=  np.loadtxt("sampledata.txt") plt.plot(data) plt.show()

loadtxt numpy delimiter=','

?

loadtxt

usecols= (1, 3)

1 2 3 4 5 6 7

a

unpack=True

import  matplotlib.pyplot  as  plt import  numpy  as  np   a,  b=  np.loadtxt("sampledata.txt",  usecols=(0,  2),  unpack=True)   plt.plot(a,  b) plt.show()

0

b

2

?

sampledata.txt 1 2 3 4 5 6 7 8

import  matplotlib.pyplot  as  plt import  numpy  as  np     data=  np.loadtxt("sampledata.txt") x=  data[1]     plt.plot(x) plt.show() data[1] 1

data x= [data[i][1] for i in range(len(data))]

3

3

?

[2, 6, 6]

1

X, Y, Z 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

3

import  numpy  as  np import  matplotlib.pyplot  as  plt import  mpl_toolkits.mplot3d.axes3d     data=  np.loadtxt("sampledata.txt")   fig  =  plt.figure() ax  =  fig.add_subplot(111,  projection='3d')   X=  [data[i][0]  for  i  in  range(len(data))] Y=  [data[i][1]  for  i  in  range(len(data))] Z=  [data[i][2]  for  i  in  range(len(data))]   ax.scatter(X,  Y,  Z)   plt.show()

N=100

x x x numpy

y

0

y y array

1 x

x

?

y

np.average(x),np.std(x)

ax.set_axis_off()