阅读背景:

Python中用matplotlib.pyplot画图总结_weixin_33893473的博客

来源:互联网 

1、

import numpy as np
import matplotlib.pyplot as plt

def f1(t):     #根据横坐标t,定义第一条曲线的纵坐标
    return np.exp(-t)*np.cos(2*np.pi*t)

def f2(t):     #根据横坐标t,定义第二条曲线的纵坐标
    return np.sin(2*np.pi*t)*np.cos(3*np.pi*t)
#定义很坐标的值,来自于np.arange(0.0,5.0,0.02),
#..表示0--5的等差数列的列表[0,0.02,0.04......4.8]不包括5.0
t = np.arange(0.0,5.0,0.02) 

#定义一个画布
plt.figure()
#参数分析:plt.plot(横坐标值,纵坐标值,"color",由legend()建的图例中的label,linewidth=)
#plt.plot()函数,将自动与距离最近的figure对应
#label的值$...$之间的公式由此页面可得https://private.codecogs.com/latex/eqneditor.php
plt.plot(t,f1(t),"g-",label="$f(t)=e^{-t} \cdot \cos (2 \pi t)$") 
plt.plot(t,f2(t),"r-.",label="$g(t)=\sin (2 \pi t) \cos (3 \pi t)$",linewidth=2)

#确定坐标轴的值、坐标轴的label,以及画布的标题
plt.axis([0.0,5.01,-1.0,1.5])
plt.xlabel("t")
plt.ylabel("v")
plt.title("a simple example")

plt.grid(True) #生成网格
plt.legend()   #产生右上角的图例   
plt.show()

###matplotlib.pyplot中的add_subplot(2,3,2)函数参数,用于分画布###
#建立一个画布fig2,可以理解为画布对象
fig2=plt.figure()

#把fig2分为:2行1列,选择第1块用。注:add_subplot(,,)函数是要有对象的,如这里用了fig2
ax=fig2.add_subplot(2,1,1)
plt.plot(t,t/3,"r-",label="$",linewidth=3)
plt.legend()
#把fig2分为:2行2列,选择第4块用。
ax=fig2.add_subplot(2,2,4)  
plt.plot(t,t/3,"b-",label="$")
plt.legend()
plt.show()import numpy as np
import matpl



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: