117 lines
3.6 KiB
Python
117 lines
3.6 KiB
Python
#===============================================================
|
||
# 画图:柱状图,散点图,拆线图,3D图,概率分布图,累计概率分布图
|
||
#===============================================================
|
||
|
||
import numpy as np
|
||
import matplotlib.pyplot as plt
|
||
|
||
#===============================================================
|
||
# 柱状图
|
||
plt.subplot(221)
|
||
size = 5
|
||
x = np.arange(size)
|
||
a = np.random.random(size)
|
||
b = np.random.random(size)
|
||
width=0.3
|
||
plt.bar(x+width/2, a, label='a',width=width)
|
||
plt.bar(x+width*3/2, b, label='b',width=width)
|
||
plt.legend()
|
||
plt.xlabel('x-axis')
|
||
plt.ylabel('y-axis')
|
||
plt.title('plot')
|
||
#plt.show()
|
||
|
||
#===============================================================
|
||
# 散点图
|
||
import matplotlib.pyplot as plt
|
||
plt.subplot(222)
|
||
#plt.figure(figsize=(6,5))
|
||
x=np.random.random(10)
|
||
y=np.random.random(10)
|
||
plt.scatter(x,y,s=15,c='b',marker=(6,1),alpha=0.7,lw=2)
|
||
plt.xlim(0,1)
|
||
plt.ylim(0,1)
|
||
#plt.show()
|
||
|
||
#===============================================================
|
||
# 拆线图
|
||
import matplotlib.pyplot as plt
|
||
plt.subplot(223)
|
||
size = 5
|
||
x = np.arange(size)
|
||
a = np.random.random(size)
|
||
b = np.random.random(size)
|
||
plt.plot(x,a, marker='o', mec='b', mfc='b',label=u'y=a')
|
||
plt.plot(x,b, marker='*', ms=10,label=u'y=b')
|
||
plt.legend()
|
||
plt.xlabel('x-axis')
|
||
plt.ylabel('y-axis')
|
||
plt.title('plot')
|
||
plt.show()
|
||
|
||
#===============================================================
|
||
# 3D图
|
||
#===============================================================
|
||
# plt.subplot(224)
|
||
from mpl_toolkits.mplot3d import Axes3D
|
||
import matplotlib.pyplot as plt
|
||
|
||
# 定义函数
|
||
def rand_range(n, vmin, vmax):
|
||
'''
|
||
make an array of random numbers having shape (n, )
|
||
with each number distributed Uniform(vmin, vmax).
|
||
'''
|
||
return (vmax - vmin) * np.random.rand(n) + vmin
|
||
|
||
fig = plt.figure()
|
||
ax = fig.add_subplot(111, projection='3d')
|
||
# plot n random points in the box
|
||
# defined by x in [23, 32], y in [0, 100], z in [zlow, zhigh].
|
||
n = 5
|
||
for c, m, zlow, zhigh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]:
|
||
xs = rand_range(n, 20, 30)
|
||
ys = rand_range(n, 30, 100)
|
||
zs = rand_range(n, zlow, zhigh)
|
||
ax.scatter(xs, ys, zs, c=c, marker=m)
|
||
|
||
# 单独一个散点
|
||
ax.scatter(25, 50, -25, c='g', marker="*")
|
||
|
||
ax.set_xlabel('x')
|
||
ax.set_ylabel('y')
|
||
ax.set_zlabel('z')
|
||
plt.show()
|
||
|
||
#===============================================================
|
||
# 概率分布图,累计概率分布图
|
||
#===============================================================
|
||
|
||
import matplotlib.pyplot as plt
|
||
import numpy as np
|
||
#概率分布直方图
|
||
#高斯分布
|
||
|
||
mean = 0 #均值为0
|
||
sigma = 1 #标准差为1,反应数据集中还是分散的值
|
||
x=mean+sigma*np.random.randn(10000)
|
||
|
||
#第二个参数是柱子宽一些还是窄一些,越大越窄越密
|
||
fig,(ax0,ax1) = plt.subplots(nrows=2,figsize=(6,6))
|
||
|
||
##pdf概率分布图,一万个数落在某个区间内的数有多少个
|
||
ax0.hist(x,bins=40,density=True,histtype='bar',facecolor='green',alpha=0.8,rwidth=0.8) # bins参数表示将数据分成几组
|
||
ax0.set_title('pdf')
|
||
|
||
#cdf累计概率函数,cumulative累计。比如需要统计小于5的数的概率
|
||
# bins参数表示将数据分成几组
|
||
# normed 是否对y轴数据进行标准化:True表是在本区间的点在所有的点中所占的概率,如果 normed 为False, 则是显示点的数量
|
||
ax1.hist(x,bins=20,density=False,histtype='step',facecolor='blue',alpha=0.8,cumulative=True,rwidth=0.8)
|
||
ax1.set_title("cdf")
|
||
fig.subplots_adjust(hspace=0.4)
|
||
plt.show()
|
||
|
||
#plt.draw()
|
||
|
||
# 可用 help(function) 查看函数帮助,如:help(ax.plot_surface)
|
||
#=============================================================== |