思路:首先需要一系列的x轴坐标,可通过numpy中的arange()函数生成,例如从0到4,步长为0.02;然后借助numpy中的正弦、余弦函数对每个x坐标分别求值;最后根据x坐标和对应的y坐标画图。
In [42]:
import matplotlib.pyplot as plt import numpy as np plt.rcParams['font.family'] ='kaiti' x = np.arange(0,4,0.02) #从0-4中以0.02为等差的等差序列,生成x轴序列 y_1 = np.sin(np.pi*x)+2 y_2 = np.cos(np.pi*x) plt.plot(x,y_1,'mx') plt.plot(x,y_2,'r') plt.legend(['正弦曲线','余弦曲线']) plt.title('正余弦曲线') plt.savefig('../R&Q_pic/test3',dpi=600) plt.show()
C:\Users\Administrator\AppData\Local\Temp\ipykernel_4764\3258683806.py:14: UserWarning: Glyph 8722 (\N{MINUS SIGN}) missing from current font. plt.savefig('../R&Q_pic/test3',dpi=600) C:\Users\Administrator\anaconda3\lib\site-packages\IPython\core\pylabtools.py:151: UserWarning: Glyph 8722 (\N{MINUS SIGN}) missing from current font. fig.canvas.print_figure(bytes_io, **kw)
In [49]:
#多区域绘图 import numpy as np import matplotlib.pyplot as plt def f(t): return np.exp(-t)*np.cos(2*np.pi*t) a=np.arange (0,5,0.02) plt. subplot(322)