首页 > 编程语言 > 详细

Python中用matplotlib.pyplot画图总结

时间:2017-07-30 23:13:06      阅读:533      评论:0      收藏:0      [点我收藏+]

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的值$...$之间的公式由此页面可得http://www.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="$11$",linewidth=3)
plt.legend()
#把fig2分为:2行2列,选择第4块用。
ax=fig2.add_subplot(2,2,4)  
plt.plot(t,t/3,"b-",label="$11$")
plt.legend()
plt.show()


本文出自 “Joe科技” 博客,请务必保留此出处http://joekeji.blog.51cto.com/12424130/1952134

Python中用matplotlib.pyplot画图总结

原文:http://joekeji.blog.51cto.com/12424130/1952134

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!