matplotlib画图

 1 import matplotlib
 2 import matplotlib.pyplot as plt
 3 import numpy as np
 4 import sys
 5 
 6 # Data for plotting
 7 t = np.arange(0.0, 0.2, 0.001)
 8 f1 = 10
 9 f2 = 20
10 s1 = np.sin(2 * np.pi * f1 * t)
11 s2 = np.sin(2 * np.pi * f2 * t)
12 
13 fig, ax = plt.subplots()
14 #ax.plot(t, s1, '-ro', t, s2, '--bD')
15 ax.plot(t, s1, label='10 Hz')
16 ax.plot(t, s2, label='20 Hz')
17 
18 ax.set(xlabel='time (s)', ylabel='unit',
19        title='sin')
20 ax.grid()
21 ax.legend()
22 #ax.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=0,
23 #    ncol=3, mode="expand", borderaxespad=0.)
24 
25 plt.savefig("test.png", dpi=500, bbox_inches='tight')
26 
27 fig1 = plt.figure()
28 plt.subplot(2, 1, 1)
29 plt.plot(t, s1, 'o-')
30 plt.title('A tale of 2 subplots')
31 plt.ylabel('Damped oscillation')
32 
33 plt.subplot(2, 1, 2)
34 plt.plot(t, s2, '.-')
35 plt.xlabel('time (s)')
36 plt.ylabel('Undamped')
37 fig1.savefig("test1.png", dpi=500, bbox_inches='tight')
38 plt.show()

 

posted @ 2020-05-23 11:36  博客园—哆啦A梦  阅读(114)  评论(0)    收藏  举报