九、直方圖

 

 

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 [ ]:
相關文章
相關標籤/搜索