9、直方图
In [ ]:
'''
直方图
将数据按几份进行统计,将每份统计到的数据再以间隙为0的柱状图展示
s.hist(
['by=None', 'ax=None', 'grid=True', 'xlabelsize=None', 'xrot=None', 'ylabelsize=None', 'yrot=None', 'figsize=None', 'bins=10', '**kwds'],
)
Docstring:
Draw histogram of the input series using matplotlib.
Parameters
----------
by : object, optional
If passed, then used to form histograms for separate groups
ax : matplotlib axis object
If not passed, uses gca()
grid : boolean, default True
Whether to show axis grid lines
xlabelsize : int, default None
If specified changes the x-axis label size
xrot : float, default None
rotation of x axis labels
ylabelsize : int, default None
If specified changes the y-axis label size
yrot : float, default None
rotation of y axis labels
figsize : tuple, default None
figure size in inches by default
bins : integer or sequence, default 10
Number of histogram bins to be used. If an integer is given, bins + 1
bin edges are calculated and returned. If bins is a sequence, gives
bin edges, including left edge of first bin and right edge of last
bin. In this case, bins is returned unmodified.
bins : integer, default 10
Number of histogram bins to be used
'''
In [5]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
In [22]:
# 直方图+密度图
s = pd.Series(np.random.randn(100))
# 直方图
s.hist(bins = 10,
histtype = 'stepfilled',
align = 'mid',
orientation = 'vertical',
alpha=0.5,
density =True)
# bin:要使用的直方图的箱数
# density 标准化
# histtype 风格,bar,barstacked,step,stepfilled
# orientation 水平还是垂直{‘horizontal’, ‘vertical’}
# align : {‘left’, ‘mid’, ‘right’}, optional(对齐方式)
s.plot(kind='kde',style='k--')
# 密度图
Out[22]:
In [27]:
# 堆叠直方图
plt.figure(num=1)
df = pd.DataFrame({'a': np.random.randn(1000) + 1, 'b': np.random.randn(1000),
'c': np.random.randn(1000) - 1, 'd': np.random.randn(1000)-2},
columns=['a', 'b', 'c','d'])
df.plot.hist(stacked=True,
bins=20,
colormap='Greens_r',
alpha=0.5,
grid=True)
# 使用DataFrame.plot.hist()和Series.plot.hist()方法绘制
# stacked:是否堆叠
df.hist(bins=100)
# 生成多个直方图
Out[27]:
In [ ]: