(生成的数据data类似于excle 有行标签 index 有列标签 columns )
1import pandas as pd
2import numpy as np
3s = pd.Series([1,3,6,np.nan,4,1]) #生成一维数组
0 1.0 1 3.0 2 6.0 3 NaN 4 4.0 5 1.0 dtype: float64
xxxxxxxxxx
31dates = pd.date_range('20160101', periods=6)#index
2df = pd.DataFrame(np.random.randn(6,4), index=dates, columns=['A','B','C','D'])# 生成6*4的数据
3print(df)
A B C D 2016-01-01 0.766013 0.709325 -0.177668 1.955910 2016-01-02 -0.458004 -1.522308 0.396516 1.358957 2016-01-03 -1.421787 0.978309 0.677042 1.498006 2016-01-04 0.028752 -1.021694 1.245495 -0.316957 2016-01-05 1.972788 -1.803018 -0.110645 -0.635439 2016-01-06 -0.265201 -0.310657 0.262895 1.532657
xxxxxxxxxx
11print(df['A'])# 与 df.A 效果相同 按列标签输出
2013-01-01 -0.162028 2013-01-02 -0.247155 2013-01-03 -1.544164 2013-01-04 -1.194200 2013-01-05 2.173732 2013-01-06 -0.501831 Freq: D, Name: A, dtype: float64
xxxxxxxxxx
11print(df['20130102':'20130104']) # 根据index范围输出 若print(df['A':'C'])则会报错
A B C D 2013-01-02 -0.247155 -1.664754 2.044796 -1.647486 2013-01-03 -1.544164 -0.278302 -0.910734 -1.471865 2013-01-04 -1.194200 -1.295544 -0.522822 0.416136
xxxxxxxxxx
41# select by label: loc 通过 x y 的label 进行选择输出
2print(df.loc['20130102'])
3print(df.loc[:,['A','B']])
4print(df.loc['20130102', ['A','B']])
A -0.247155 B -1.664754 C 2.044796 D -1.647486 Name: 2013-01-02 00:00:00, dtype: float64 A B 2013-01-01 -0.162028 -1.209992 2013-01-02 -0.247155 -1.664754 2013-01-03 -1.544164 -0.278302 2013-01-04 -1.194200 -1.295544 2013-01-05 2.173732 0.422870 2013-01-06 -0.501831 0.392393 A -0.247155 B -1.664754 Name: 2013-01-02 00:00:00, dtype: float64
xxxxxxxxxx
51# select by position: iloc 通过位置选择 即通过坐标选择(或范围)
2print(df.iloc[3])
3print(df.iloc[3, 1])
4print(df.iloc[3:5,0:2])
5print(df.iloc[[1,2,4],[0,2]])
A -1.194200 B -1.295544 C -0.522822 D 0.416136 Name: 2013-01-04 00:00:00, dtype: float64 -1.2955436468567092 A B 2013-01-04 -1.194200 -1.295544 2013-01-05 2.173732 0.422870 A C 2013-01-02 -0.247155 2.044796 2013-01-03 -1.544164 -0.910734 2013-01-05 2.173732 -0.665288
x1# mixed selection: ix 混合选择 即通过label 和坐标 进行选择
2
3print(df.ix[:3, ['A', 'C']])
4
5# Boolean indexing 逻辑选择 可以加入一些逻辑判断进行选择
6
7print(df[df.A > 0])
A C 2013-01-01 -0.162028 -1.259382 2013-01-02 -0.247155 2.044796 2013-01-03 -1.544164 -0.910734 A B C D 2013-01-05 2.173732 0.42287 -0.665288 -0.585846
xxxxxxxxxx
71#与上述差不多 还是确定要改变的数据位置即可
2df.iloc[2,2] = 1111# 通过坐标位置改变
3df.loc['2013-01-03', 'D'] = 2222 #通过标签确定位置 改变
4df.A[df.A>0] = 0 #通过逻辑判断筛选改变
5df['F'] = np.nan #通过标签改变
6df['G'] = pd.Series([1,2,3,4,5,6], index=pd.date_range('20130101', periods=6))# 666
7print(df)
xxxxxxxxxx
71A B C D F G
22013-01-01 0.000000 -0.132451 0.021797 -1.656566 NaN 1
32013-01-02 -0.182227 -0.656873 -0.633688 1.143701 NaN 2
42013-01-03 0.000000 -0.330042 1111.000000 2222.000000 NaN 3
52013-01-04 -0.664301 0.248035 -1.286524 0.879787 NaN 4
62013-01-05 -0.005037 -0.753467 -1.390868 0.842121 NaN 5
72013-01-06 -0.529367 -1.093037 -0.984761 -1.243129 NaN 6
xxxxxxxxxx
71dates = pd.date_range('20130101', periods=6)
2df = pd.DataFrame(np.arange(24).reshape((6,4)), index=dates, columns=['A', 'B', 'C', 'D'])
3# 先生成几个nan
4df.iloc[0,1] = np.nan
5df.iloc[1,2] = np.nan
6df.iloc[2,:] = np.nan
7print(df)
xxxxxxxxxx
71A B C D
22013-01-01 0.0 NaN 2.0 3.0
32013-01-02 4.0 5.0 NaN 7.0
42013-01-03 NaN NaN NaN NaN
52013-01-04 12.0 13.0 14.0 15.0
62013-01-05 16.0 17.0 18.0 19.0
72013-01-06 20.0 21.0 22.0 23.0
xxxxxxxxxx
11print(df.dropna(axis=0, how='any')) # how={'any', 'all'} 删除任何有nan的行 (axis=1则为列)
xxxxxxxxxx
41A B C D
22013-01-04 12.0 13.0 14.0 15.0
32013-01-05 16.0 17.0 18.0 19.0
42013-01-06 20.0 21.0 22.0 23.0
xxxxxxxxxx
11print(df.dropna(axis=0, how='all')) # how={'any', 'all'} 删除全为nan的行 (axis=1则为列)
xxxxxxxxxx
61A B C D
22013-01-01 0.0 NaN 2.0 3.0
32013-01-02 4.0 5.0 NaN 7.0
42013-01-04 12.0 13.0 14.0 15.0
52013-01-05 16.0 17.0 18.0 19.0
62013-01-06 20.0 21.0 22.0 23.0
xxxxxxxxxx
11print(df.fillna(value=0)) #用其它值 填充 nan
xxxxxxxxxx
71A B C D
22013-01-01 0.0 0.0 2.0 3.0
32013-01-02 4.0 5.0 0.0 7.0
42013-01-03 0.0 0.0 0.0 0.0
52013-01-04 12.0 13.0 14.0 15.0
62013-01-05 16.0 17.0 18.0 19.0
72013-01-06 20.0 21.0 22.0 23.0
xxxxxxxxxx
11print(pd.isnull(df)) #判断是不是non
xxxxxxxxxx
71A B C D
22013-01-01 False True False False
32013-01-02 False False True False
42013-01-03 True True True True
52013-01-04 False False False False
62013-01-05 False False False False
72013-01-06 False False False False
可根据columns列合并和根据index索引合并
数据 的index可以不写 默认为0 1 2 3....,但是列columns一定要写(写成字典的形式)
xxxxxxxxxx
81import matplotlib.pyplot as plt
2import numpy as np
3#基本用法
4x = np.linspace(-5, 5, 50)
5y = 2*x + 1
6# y = x**2
7plt.plot(x, y)
8plt.show()
xxxxxxxxxx
141x = np.linspace(-3, 3, 50)
2y1 = 2*x + 1
3y2 = x**2
4##使用figure 来自定义自己窗口
5#第一个figure figure下面的图都在该figure上绘出 直到定义下一个figure
6plt.figure()
7plt.plot(x, y1)
8
9#第二个figure num是图片的的索引
10plt.figure(num=3, figsize=(8, 5),)
11plt.plot(x, y2)#第一条曲线
12
13plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--')#第二条曲线
14plt.show()
xxxxxxxxxx
231x = np.linspace(-3, 3, 50)
2y1 = 2*x + 1
3y2 = x**2
4
5plt.figure()
6plt.plot(x, y2)
7# plot the second curve in this figure with certain parameters
8plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--')
9# set x limits 设置x y的范围
10plt.xlim((-1, 2))
11plt.ylim((-2, 3))
12plt.xlabel('I am x')# x y 的label
13plt.ylabel('I am y')
14
15# set new sticks #设置新的刻度
16new_ticks = np.linspace(-1, 2, 5)
17print(new_ticks)
18plt.xticks(new_ticks)
19# to use '$ $' for math text and nice looking, e.g. '$\pi$'
20# set tick labels 设置新的label $是为了让字体更加美观 |为转义字符 可以将alpha转为数学里面的α
21plt.yticks([-2, -1.8, -1, 1.22, 3],
22 [r'$reallybad$', r'$bad\alpha$', r'$normal$', r'$good$', r'$really\ good$'])
23plt.show()
xxxxxxxxxx
11[-1. -0.25 0.5 1.25 2. ]
xxxxxxxxxx
121x = np.linspace(-3, 3, 50)
2y2 = x**2
3plt.figure(num=3, figsize=(8, 5),)
4plt.plot(x, y2)#第一条曲线
5plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--')#第二条曲线
6
7
8# gca = 'get current axis'
9ax= plt.gca()
10ax.spines['right'].set_color('none')# 将上边和右边的边框去掉
11ax.spines['top'].set_color('none')
12plt.show()
xxxxxxxxxx
231x = np.linspace(-3, 3, 50)
2y2 = x**2
3plt.figure(num=3, figsize=(8, 5),)
4plt.plot(x, y2)#第一条曲线
5plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--')#第二条曲线
6
7
8ax= plt.gca()
9ax.spines['right'].set_color('none')# 将上边和右边的边框去掉
10ax.spines['top'].set_color('none')
11ax.xaxis.set_ticks_position('bottom')# 把下边的那条变设置为x_axis
12# ACCEPTS: [ 'top' | 'bottom' | 'both' | 'default' | 'none' ]
13
14ax.spines['bottom'].set_position(('data', 0))# 把bottom边放在y==0的高度
15# the 1st is in 'outward' | 'axes' | 'data'
16# axes: percentage of y axis
17# data: depend on y data
18
19ax.yaxis.set_ticks_position('left') #把左边的那条边设为 y_axis
20# ACCEPTS: [ 'left' | 'right' | 'both' | 'default' | 'none' ]
21
22ax.spines['left'].set_position(('data',0))# 把left 放在x==0的位置
23plt.show()
xxxxxxxxxx
401x = np.linspace(-3, 3, 50)
2y1 = 2*x + 1
3y2 = x**2
4
5plt.figure()
6# set x limits
7plt.xlim((-1, 2))
8plt.ylim((-2, 3))
9
10# set new sticks
11new_sticks = np.linspace(-1, 2, 5)
12plt.xticks(new_sticks)
13# set tick labels
14plt.yticks([-2, -1.8, -1, 1.22, 3],
15 [r'$really\ bad$', r'$bad$', r'$normal$', r'$good$', r'$really\ good$'])
16# 设置图例
17l1, = plt.plot(x, y1, label='linear line')
18l2, = plt.plot(x, y2, color='red', linewidth=1.0, linestyle='--', label='square line')
19#设置图例的位置
20plt.legend(loc='upper right')
21#plt.legend(handles=[l1, l2], labels=['up', 'down'], loc='best')
22# the "," is very important in here l1, = plt... and l2, = plt... for this step
23"""legend( handles=(line1, line2, line3),
24 labels=('label1', 'label2', 'label3'),
25 'upper right')
26 The *loc* location codes are::
27
28 'best' : 0, (currently not supported for figure legends)
29 'upper right' : 1,
30 'upper left' : 2,
31 'lower left' : 3,
32 'lower right' : 4,
33 'right' : 5,
34 'center left' : 6,
35 'center right' : 7,
36 'lower center' : 8,
37 'upper center' : 9,
38 'center' : 10,"""
39
40plt.show()
xxxxxxxxxx
321x = np.linspace(-3, 3, 50)
2y = 2*x + 1
3
4plt.figure(num=1, figsize=(8, 5),)
5plt.plot(x, y,)
6
7ax = plt.gca()
8ax.spines['right'].set_color('none')
9ax.spines['top'].set_color('none')
10ax.spines['top'].set_color('none')
11ax.xaxis.set_ticks_position('bottom')
12ax.spines['bottom'].set_position(('data', 0))
13ax.yaxis.set_ticks_position('left')
14ax.spines['left'].set_position(('data', 0))
15
16x0 = 1
17y0 = 2*x0 + 1
18plt.plot([x0, x0,], [0, y0,], 'k--', linewidth=2.5)# 画黑色的虚线 k--表示黑色
19plt.scatter([x0, ], [y0, ], s=50, color='b')# 画出该点 b 蓝色
20
21# method 1:
22#####################
23plt.annotate(r'$2x+1=%s$' % y0, xy=(x0, y0), xycoords='data', xytext=(+30, -30),
24 textcoords='offset points', fontsize=16,
25 arrowprops=dict(arrowstyle='->', connectionstyle="arc3,rad=.2"))
26
27# method 2:
28########################
29plt.text(-3.7, 3, r'$This\ is\ the\ some\ text. \mu\ \sigma_i\ \alpha_t$',
30 fontdict={'size': 16, 'color': 'r'})
31
32plt.show()
xxxxxxxxxx
201x = np.linspace(-3, 3, 50)
2y = 0.1*x
3
4plt.figure()
5plt.plot(x, y, linewidth=10, zorder=1) # set zorder for ordering the plot in plt 2.0.2 or higher
6plt.ylim(-2, 2)
7ax = plt.gca() #获得当前轴
8ax.spines['right'].set_color('none')
9ax.spines['top'].set_color('none')
10ax.xaxis.set_ticks_position('bottom')
11ax.spines['bottom'].set_position(('data', 0))
12ax.yaxis.set_ticks_position('left')
13ax.spines['left'].set_position(('data', 0))
14
15
16for label in ax.get_xticklabels() + ax.get_yticklabels():
17 label.set_fontsize(12)
18 # set zorder for ordering the plot in plt 2.0.2 or higher
19 label.set_bbox(dict(facecolor='white', edgecolor='none', alpha=0.8, zorder=2))
20plt.show()
xxxxxxxxxx
131n = 1024 # data size
2X = np.random.normal(0, 1, n)
3Y = np.random.normal(0, 1, n)
4T = np.arctan2(Y, X) # for color later on
5
6plt.scatter(X, Y, s=75, c=T, alpha=.5)# s为size c为color alpha 为透明度
7
8plt.xlim(-1.5, 1.5)
9plt.xticks(()) # ignore xticks
10plt.ylim(-1.5, 1.5)
11plt.yticks(()) # ignore yticks 隐藏所有的ticks
12
13plt.show()
xxxxxxxxxx
241n = 12
2X = np.arange(n)#生成从0-n 12个整数
3Y1 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n)#产生n个0.5-1的数
4Y2 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n)
5
6plt.bar(X, +Y1, facecolor='#9999ff', edgecolor='white')
7plt.bar(X, -Y2, facecolor='#ff9999', edgecolor='white')
8
9for x, y in zip(X, Y1):
10 # ha: horizontal alignment
11 # va: vertical alignment
12 plt.text(x + 0.1, y + 0.05, '%.2f' % y, ha='center', va='bottom')
13
14for x, y in zip(X, Y2):
15 # ha: horizontal alignment
16 # va: vertical alignment
17 plt.text(x + 0.4, -y - 0.05, '%.2f' % y, ha='center', va='top')
18
19plt.xlim(-.5, n)
20plt.xticks(())
21plt.ylim(-1.25, 1.25)
22plt.yticks(())
23
24plt.show()
xxxxxxxxxx
211def f(x,y):
2 # the height function
3 return (1 - x / 2 + x**5 + y**3) * np.exp(-x**2 -y**2)
4
5n = 256
6x = np.linspace(-3, 3, n)
7y = np.linspace(-3, 3, n)
8X,Y = np.meshgrid(x, y)#网格图
9
10# 1. use plt.contourf to filling contours 填充
11# X, Y and value for (X,Y) point
12plt.contourf(X, Y, f(X, Y), 8, alpha=.75, cmap=plt.cm.hot)# 类型 冷热图
13
14# 2. use plt.contour to add contour lines 画线
15C = plt.contour(X, Y, f(X, Y), 8, colors='black')#8 代表等高线条数 即等高线的稀疏
16# 3. adding label
17plt.clabel(C, inline=True, fontsize=10)# inline 为Flase时 label将被线挡住
18
19plt.xticks(())
20plt.yticks(())
21plt.show()
xxxxxxxxxx
171# image data
2a = np.array([0.313660827978, 0.365348418405, 0.423733120134,
3 0.365348418405, 0.439599930621, 0.525083754405,
4 0.423733120134, 0.525083754405, 0.651536351379]).reshape(3,3)
5
6"""
7for the value of "interpolation", check this:
8http://matplotlib.org/examples/images_contours_and_fields/interpolation_methods.html
9for the value of "origin"= ['upper', 'lower'], check this:
10http://matplotlib.org/examples/pylab_examples/image_origin.html
11"""
12plt.imshow(a, interpolation='nearest', cmap='bone', origin='lower')#
13plt.colorbar(shrink=.92)# 压缩为92%
14
15plt.xticks(())# 隐藏 ticks
16plt.yticks(())
17plt.show()
xxxxxxxxxx
471from mpl_toolkits.mplot3d import Axes3D
2
3fig = plt.figure()# 建一个图片的窗口
4ax = Axes3D(fig)# 给窗口添加3D维度
5# X, Y value
6X = np.arange(-4, 4, 0.25)
7Y = np.arange(-4, 4, 0.25)
8X, Y = np.meshgrid(X, Y)# 作为网格
9R = np.sqrt(X ** 2 + Y ** 2)
10# height value
11Z = np.sin(R)
12
13ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.get_cmap('rainbow'))# 1 1分别为行跨 列跨
14"""
15============= ================================================
16 Argument Description
17 ============= ================================================
18 *X*, *Y*, *Z* Data values as 2D arrays
19 *rstride* Array row stride (step size), defaults to 10
20 *cstride* Array column stride (step size), defaults to 10
21 *color* Color of the surface patches
22 *cmap* A colormap for the surface patches.
23 *facecolors* Face colors for the individual patches
24 *norm* An instance of Normalize to map values to colors
25 *vmin* Minimum value to map
26 *vmax* Maximum value to map
27 *shade* Whether to shade the facecolors
28 ============= ================================================
29"""
30
31# I think this is different from plt12_contours
32ax.contourf(X, Y, Z, zdir='z', offset=-2, cmap=plt.get_cmap('rainbow'))
33"""
34========== ================================================
35 Argument Description
36 ========== ================================================
37 *X*, *Y*, Data values as numpy.arrays
38 *Z*
39 *zdir* The direction to use: x, y or z (default)
40 *offset* If specified plot a projection of the filled contour
41 on this position in plane normal to zdir
42 ========== ================================================
43"""
44
45ax.set_zlim(-2, 2)
46
47plt.show()
xxxxxxxxxx
171# example 1:
2###############################
3plt.figure(figsize=(6, 4))
4# plt.subplot(n_rows, n_cols, plot_num)
5plt.subplot(2, 2, 1) #两行两列 第一张图
6plt.plot([0, 1], [0, 1])
7
8plt.subplot(222)#也可将逗号省掉 两行两列 第二张图
9plt.plot([0, 1], [0, 2])
10
11plt.subplot(223)#两行两列 第三张图
12plt.plot([0, 1], [0, 3])
13
14plt.subplot(224)#两行两列 第四张图
15plt.plot([0, 1], [0, 4])
16
17plt.tight_layout()
xxxxxxxxxx
231# example 2:
2###############################
3plt.figure(figsize=(6, 4))
4# plt.subplot(n_rows, n_cols, plot_num)
5plt.subplot(2, 1, 1)
6# figure splits into 2 rows, 1 col, plot to the 1st sub-fig
7plt.plot([0, 1], [0, 1])
8
9plt.subplot(234)
10# figure splits into 2 rows, 3 col, plot to the 4th sub-fig
11plt.plot([0, 1], [0, 2])
12
13plt.subplot(235)
14# figure splits into 2 rows, 3 col, plot to the 5th sub-fig
15plt.plot([0, 1], [0, 3])
16
17plt.subplot(236)
18# figure splits into 2 rows, 3 col, plot to the 6th sub-fig
19plt.plot([0, 1], [0, 4])
20
21
22plt.tight_layout()
23plt.show()
xxxxxxxxxx
201import matplotlib.pyplot as plt
2import matplotlib.gridspec as gridspec
3
4# method 1: subplot2grid
5##########################
6plt.figure()
7# stands for axes 三行三列 第一图从(0,0)开始 跨度为3(默认为1)
8ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3)
9ax1.plot([1, 2], [1, 2])
10ax1.set_title('ax1_title')# 设置title 的label
11ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2)# 从(1,0)开始 列跨度为2
12ax3 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)#从(1,2)开始 行跨度为2
13ax4 = plt.subplot2grid((3, 3), (2, 0))# 不写跨度 默认 1 1
14ax4.scatter([1, 2], [2, 2])
15ax4.set_xlabel('ax4_x')
16ax4.set_ylabel('ax4_y')
17ax5 = plt.subplot2grid((3, 3), (2, 1))
18
19
20
xxxxxxxxxx
121# method 2: gridspec
2#########################
3plt.figure()
4gs = gridspec.GridSpec(3, 3)
5# use index from 0
6ax6 = plt.subplot(gs[0, :])
7ax7 = plt.subplot(gs[1, :2])
8ax8 = plt.subplot(gs[1:, 2])
9ax9 = plt.subplot(gs[-1, 0])
10ax10 = plt.subplot(gs[-1, -2])
11
12
xxxxxxxxxx
71# method 3: easy to define structure
2####################################
3f, ((ax11, ax12), (ax13, ax14)) = plt.subplots(2, 2, sharex=True, sharey=True)
4ax11.scatter([1,2], [1,2])
5
6plt.tight_layout()
7plt.show()
xxxxxxxxxx
281fig = plt.figure()
2x = [1, 2, 3, 4, 5, 6, 7]
3y = [1, 3, 4, 2, 5, 8, 6]
4
5# below are all percentage
6left, bottom, width, height = 0.1, 0.1, 0.8, 0.8# 小图相对于figure 的位置大小 (百分比)
7ax1 = fig.add_axes([left, bottom, width, height]) # main axes 第一个图
8ax1.plot(x, y, 'r')
9ax1.set_xlabel('x')
10ax1.set_ylabel('y')
11ax1.set_title('title')
12
13ax2 = fig.add_axes([0.2, 0.6, 0.25, 0.25]) # inside axes 第二个小图
14ax2.plot(y, x, 'b')
15ax2.set_xlabel('x')
16ax2.set_ylabel('y')
17ax2.set_title('title inside 1')
18
19
20# different method to add axes
21####################################
22plt.axes([0.6, 0.2, 0.25, 0.25])
23plt.plot(y[::-1], x, 'g')
24plt.xlabel('x')
25plt.ylabel('y')
26plt.title('title inside 2')
27
28plt.show()
xxxxxxxxxx
161x = np.arange(0, 10, 0.1)
2y1 = 0.05 * x**2
3y2 = -1 *y1
4
5fig, ax1 = plt.subplots()
6
7ax2 = ax1.twinx() # mirror the ax1 把ax1 的数据镜像
8ax1.plot(x, y1, 'g-')
9ax2.plot(x, y2, 'b-')
10
11ax1.set_xlabel('X data')
12ax1.set_ylabel('Y1 data', color='g')
13ax2.set_ylabel('Y2 data', color='b')
14
15plt.show()
16
xxxxxxxxxx
321from matplotlib import animation
2
3fig, ax = plt.subplots()
4
5x = np.arange(0, 2*np.pi, 0.01)
6line, = ax.plot(x, np.sin(x))
7
8
9def animate(i):
10 line.set_ydata(np.sin(x + i/10.0)) # update the data
11 return line,
12
13
14# Init only required for blitting to give a clean slate.
15def init():
16 line.set_ydata(np.sin(x))
17 return line,
18
19# call the animator. blit=True means only re-draw the parts that have changed.
20# blit=True dose not work on Mac, set blit=False
21# interval= update frequency
22ani = animation.FuncAnimation(fig=fig, func=animate, frames=100, init_func=init,
23 interval=20, blit=False)
24
25# save the animation as an mp4. This requires ffmpeg or mencoder to be
26# installed. The extra_args ensure that the x264 codec is used, so that
27# the video can be embedded in html5. You may need to adjust this for
28# your system: for more information, see
29# http://matplotlib.sourceforge.net/api/animation_api.html
30# anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])
31
32plt.show()