import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
from pandas random import randn
from IPython.
%matplotlib inline
直方图:
hist()的参数
bins
可以是一个bin数量的整数值,也可以是表示bin的一个序列。默认值为10
range
bin的范围,当bins参数为序列时,此参数无效。范围外的值将被忽略掉,默认值为None
normed
如果值为True,直方图的值将进行归一化处理,形成概率密度,默认值为False
histtype
bar
默认为bar类型的直方图
barstacked
用于多种数据的堆叠直方图
step
创建未填充的线形图
stepfilled
创建默认填充的线形图
align
用于bin边界之间矩形条的居中设置。默认值为mid,其他值为left和right
color
指定直方图的颜色。可以是单一颜色值或颜色的序列。如果指定了多个数据集合,颜色序列将会设置为相同的顺序。如果未指定,将会使用一个默认的线条颜色
orientation
通过设置orientation为horizontal创建水平直方图。默认值为vertical
y=np.random.randn(1000)
x=np.arange(7)
ax=plt.gca() #获取当前列表
ax.hist(y)
ax.set_xlim(-3,3)
ax.set_xlabel('X')
ax.set_ylabel('y')
ax.set_title('first test of hist')
总结:直方图hist()内部只有一个参数y,用于显示某一输入数据的正态分布图
条形图:
条形图分为bar(),barh():水平条形图
plt.bar(left, height, width=0.8, bottom=None, hold=None, data=None, **kwargs)
left : sequence of scalars
the x coordinates of the left sides of the bars #设置为X的值
height : sequence of scalars
the heights of the bars #相当于y的值
bottom : scalar or array-like, optional
the y coordinate(s) of the bars
default: None
例题:
1:#绘制有误差条的并列条形图
data1=10*np.random.rand(5)
data2=10*np.random.rand(5)
data3=10*np.random.rand(5)
e2=0.5*np.abs(np.random.randn(len(data2)))
locs=np.arange(1,len(data1)+1)
width=0.27
plt.bar(locs,data1,width=width)
plt.bar(locs+width,data2,yerr=e2,width=width,color='g')
plt.bar(locs+2*width,data3,width=width,color='r')
plt.xticks(locs+width*1.5,locs)