matplotlib绘图

 1 import numpy as np
 2 import matplotlib.pyplot as plt
 3 
 4 x = np.linspace(0, 2 * np.pi, 50)
 5 y = np.sin(x)
 6 
 7 plt.figure(figsize=(6, 6))
 8 plt.plot(x, y, 'g*-', label='sin(x)')
 9 plt.plot(x, y * 2, 'm--', label='2sin(x)')
10 
11 plt.xlim((0, 2 * np.pi + 0.5)) #x轴范围
12 plt.xticks((0, np.pi * 0.5, np.pi, np.pi * 1.5, np.pi * 2)) #x轴刻度
13 plt.ylim((-3, 3))
14 plt.xlabel('X')
15 plt.ylabel('Y')
16 
17 #插入标注
18 x0 = np.pi
19 y0 = 0
20 
21 plt.scatter(x0, y0, color='red', s=60)
22 
23 plt.annotate('sin(np.pi)=%s'%y0, xy=(np.pi, 0), xycoords='data',
24     xytext=(+30, -30), textcoords='offset points', fontsize=16,
25     arrowprops=dict(arrowstyle='->', connectionstyle='arc3, rad=.2'))
26 
27 #plt.text(0.5, -0.25, 'sin(np.pi)=0', fontdict={'size':16, 'color':'r'})
28 
29 plt.title('sin(x) & sin(2*x)')
30 plt.legend(loc='best')
31 plt.show()
32 
33 
34 
35 plt.figure(2)
36 ax1 = plt.subplot(2, 2, 1)
37 plt.plot(x, np.sin(x), 'r')
38 
39 ax2 = plt.subplot(2, 2, 2, sharey=ax1)
40 plt.plot(x, 2 * np.sin(x), 'g')
41 
42 ax3 = plt.subplot(2, 2, 3)
43 plt.plot(x, np.cos(x), 'b')
44 
45 ax4 = plt.subplot(2,2,4, sharey=ax3)
46 plt.plot(x, 2*np.cos(x), 'y')
47 
48 plt.show()
49 
50 
51 
52 
53 
54 plt.figure(3)
55 
56 ax1 = plt.subplot(2, 1, 1)
57 plt.plot(x, np.sin(x), 'r')
58 
59 ax2 = plt.subplot(2, 3, 4)
60 plt.plot(x, 2 * np.sin(x), 'g')
61 
62 ax3 = plt.subplot(2, 3, 5, sharey=ax2)
63 plt.plot(x, np.cos(x), 'b')
64 
65 ax4 = plt.subplot(2,3, 6, sharey=ax2)
66 plt.plot(x, 2*np.cos(x), 'y')
67 
68 plt.show()

 

posted @ 2020-05-05 15:40  WWBlog  阅读(115)  评论(0编辑  收藏  举报