Python數據分析實戰【第三章】3.9-Matplotlib直方圖【python】

1.直方圖+密度圖


s = pd.Series(np.random.randn(1000))
s.hist(bins = 20,
       histtype = 'bar',
       align = 'mid',
       orientation = 'vertical',
       alpha=0.5,
       normed =True)
# bin:箱子的寬度
# normed 標準化,配合密度圖
# histtype 風格,bar,barstacked,step,stepfilled
# orientation 水平還是垂直{‘horizontal’, ‘vertical’}
# align : {‘left’, ‘mid’, ‘right’}, optional(對齊方式)

s.plot(kind='kde',style='k--')
# 密度圖

在這裏插入圖片描述

2.堆疊直方圖:plt.plot.hixt(stacked=True)


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=50)
# 生成多個直方圖

在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章