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]:
<matplotlib.axes._subplots.AxesSubplot at 0x1c8d4824518>
 
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]:
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x000001C8D67ECB70>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000001C8D68129B0>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x000001C8D6843080>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000001C8D6869710>]],
      dtype=object)
 
<Figure size 432x288 with 0 Axes>
 
 
In [ ]:
 
posted @ 2019-04-16 13:38  慕沁  阅读(184)  评论(0)    收藏  举报