1 import matplotlib.pyplot as plt
2
3 fig = plt.figure()
4 x = [1, 2, 3, 4, 5, 6, 7]
5 y = [1, 3, 4, 2, 5, 8, 6]
6
7 # below are all percentage
8 left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
9 ax1 = fig.add_axes([left, bottom, width, height]) # main axes
10 ax1.plot(x, y, 'r')
11 #关于plot()函数参数设置,参考博文 https://blog.csdn.net/cjcrxzz/article/details/79627483
12 ax1.set_xlabel('x')
13 ax1.set_ylabel('y')
14 ax1.set_title('title')
15
16 # inside axes
17 ax2 = fig.add_axes([0.2, 0.6, 0.25, 0.25])
18 ax2.plot(y, x, 'b')
19 ax2.set_xlabel('x')
20 ax2.set_ylabel('y')
21 ax2.set_title('title inside 1')
22
23
24 # different method to add axes
25 ####################################
26 plt.axes([0.6, 0.2, 0.25, 0.25])
27 plt.plot(y[::-1], x, 'g')
28 #y[::-1],sequenceDiagram[start:end:step],for value in rang(10)[::-1] #会将涉及的数字倒序输出
29 #a[i:j:s]
30 # 当i缺省时,默认为0,即 a[:3]相当于 a[0:3]
31 # 当j缺省时,默认为len(alist), 即a[1:]相当于a[1:10]
32 # 当i,j都缺省时,a[:]就相当于完整复制一份a了,s表示步进,缺省为1.
33 # 所以a[i:j:1]相当于a[i:j]
34 # 当s<0时,i缺省时,默认为-1. j缺省时,默认为-len(a)-1
35 # a[::-1]相当于 a[-1:-len(a)-1:-1],也就是从最后一个元素到第一个元素复制一遍,所以得到的就是一个倒序
36 plt.xlabel('x')
37 plt.ylabel('y')
38 plt.title('title inside 2')
39
40 plt.show()
1 import matplotlib.pyplot as plt
2 import numpy as np
3
4
5 #不同数据共用X轴,
6 x = np.arange(0, 10, 0.1)
7 y1 = 0.05 * x**2
8 y2 = - x + 1
9
10 fig, ax1 = plt.subplots()
11
12 ax2 = ax1.twinx() # mirror the ax1
13 ax1.plot(x, y1, 'g-')
14 ax2.plot(x, y2, 'b-')
15
16 ax1.set_xlabel('X data')
17 ax1.set_ylabel('Y1 data', color='g')
18 ax2.set_ylabel('Y2 data', color='b')
19
20 plt.show()
1 import numpy as np
2 from matplotlib import pyplot as plt
3 from matplotlib import animation #导入画动画的模块
4
5 fig, ax = plt.subplots()
6
7 x = np.arange(0, 2*np.pi, 0.01)
8 line, = ax.plot(x, np.sin(x))
9
10
11 def animate(i):
12 line.set_ydata(np.sin(x + i/10.0)) # update the data
13 return line,
14
15 # Init only required for blitting to give a clean slate.
16 def init():
17 line.set_ydata(np.sin(x))
18 return line,
19
20 ani = animation.FuncAnimation(fig=fig, func=animate, frames=500, init_func=init,
21 interval=20, blit=True)
22
23 #函数FuncAnimation(fig,func,frames,init_func,interval,blit)是绘制动图的主要函数,其参数如下:
24 # a.fig 绘制动图的画布名称
25 # b.func自定义动画函数,即程序中定义的函数animate
26 # c.frames动画长度,一次循环包含的帧数,在函数运行时,其值会传递给函数animate(i)的形参“i”
27 # d.init_func自定义开始帧,即传入刚定义的函数init,初始化函数
28 # e.interval更新频率,以ms计
29 # f.blit选择更新所有点,还是仅更新产生变化的点。应选择True,但mac用户请选择False,否则无法显
30
31 plt.show()
32 #关于动画绘制的相关内容参考博文 https://www.cnblogs.com/zhouzhe-blog/p/9614360.html