分享

Python中用matplotlib.pyplot画图总结

 PlanBlank 2018-05-31

1、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
= np.arange(0.0,5.0,0.02
#定义一个画布
plt.figure()
#参数分析:plt.plot(横坐标值,纵坐标值,"color",由legend()建的图例中的label,linewidth=)
#plt.plot()函数,将自动与距离最近的figure对应
#label的值$...$之间的公式由此页面可得http://www./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的文件,将设置自己的路径(记得把#去掉),如图:

wKiom1mRAqnzJMHZAABJw3t6h7A379.jpg

     (3)重新启动,即可,键入:jupyter notebook。

3、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# -*- 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()

wKioL1nHVZCSPDiUAAC_is8B9mg703.jpg

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多