matplotlib绘图实战

1.绘制直线

import matplotlib.pyplot as plt
import numpy as np


#讲-1 到1的数据区间等分成50份
x = np.linespace(-1,1,50)
#直线公式
 y = 2*x+1
#绘图
plt.plot(x,y)
#说明
plt.xlabel('x')
plt.ylaabel('2x+1')
#显示
plt.show()

2.绘制曲线

import matplotlib.pyplot as plt
import numpy as np


#讲-1 到1的数据区间等分成50份
x = np.linespace(-1,1,50)
#曲线公式
y =x**3
#绘图
plt.plot(x,y)
#说明
plt.xlabel('x')
plt.ylaabel('2**3')
#显示
plt.show()

3.绘制线上的点

import matplotlib.pyplot as plt


plt.plot(range(3,33,3),'ro')
plt.show()

4.绘制多条线上的点

import numpy as np
import matplotlib.pyplot as plt


#0到5之间每个0.2取一个数
t =  np.arange(0.,5.,0.2)
#红色的破折号,蓝色的方块,绿色的三角形
plt.plot(t,t,'r--',t,t**2,'bs',t,t**3,'g^')
plt.show()

5.多图形(figures)和多坐标系(axes)

import numpy as np
import matplotlib.pyplot as plt


def f(t):
    return np.exp(-t)*np.cos(2*np.pi*t)

t1 = np.arange(0.0,5.0,0.1)
t2 = np.arange(0.0,5.0,0.02)

plt.figure("2sugbplot")
plt.subplot(211)
plt.plot(t1,f(t1),'bo',t2,f(t2),'k')

plt.subplot(212)
plt.plot(t2,np.cos(2*np.pi*t2),'r--')
plt.show()

6.绘制直方图

import numpy as np
import matplotlib.pyplot as plt


#正态分布是曲线: u 与 σ 是正态分布的两个参数;
#其中u是位置参数,用来决定曲线在横轴的位置
# σ是形状参数,用来决定曲线的高矮胖瘦的程度
# σ 越大,曲线越矮,越平缓,尾部翘得越高
# mu: u(数学期望,位置参数,用来决定曲线在横轴的位置)
#sigma: σ(均值,最可能出现的结果)
 mu,sigma = 100,15
x = mu+sigma*np.random.randon(10000)

#直方图
n,bins,patches = plt.hist(x,50,normed=True,facecolor='g',alpha=0.75)
#智商
plt.xlabel("smart")
#概率
plt.ylabel('Probability')
#IO直方图
plt.title("Histogram of IQ")
plt.text(60,.025,r'$\mu=100,\ \sigma=15$')
plt.axis([40,160,0,0.03])
plt.gird(True)
plt.show()