您的位置:首页 > 编程语言 > Python开发

Python中用matplotlib.pyplot画图总结

2017-07-30 19:27 555 查看
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()
2、修改jupyter notebook的新建文件默认目录:
(1)、cmd中键入:jupyter notebook --generate-config
系统的返回结果会是一个路径。
(2)、打开..\upyter_notebook_config.py的文件,将设置自己的路径(记得把#去掉),如图:


(3)重新启动,即可,键入:jupyter notebook。
3、
# -*- coding: utf-8 -*-

import matplotlib.pylab as mtp
import numpy.random as nprd

#子图 subplot() 行、列,当前所在区域
#汇3个图,上面2个,下面一个
#左上角图
mtp.subplot(2,2,1)
x1=nprd.random_integers(10,20,50)
y1=nprd.random_integers(10,30,50)
mtp.plot(x1,y1,'o')
mtp.title("open widy")

#右上角图
mtp.subplot(2,2,2)
x2=[1,3,5,7,9]
mtp.hist(x2,color='b')
mtp.title("spy der")

#下部图
mtp.subplot(2,1,2)
x3=nprd.normal(50,10,1000)
y3=nprd.normal(100,20,1000)
mtp.plot(x3,y3,'-')
mtp.title("amt tol")
mtp.show()


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息