使用hist方法來繪制直方圖:


繪制直方圖,最主要的是一個數據集data和需要划分的區間數量bins,另外你也可以設置一些顏色、類型參數:
plt.hist(np.random.randn(1000), bins=30,normed=True, alpha=0.5, histtype='stepfilled', color='steelblue', edgecolor='none')
histtype直方圖的類型,可以是'bar'、 'barstacked'、'step'和'stepfilled'。
有時候我們也會做一些直方對比圖:


除了一維的直方圖,還可以使用hist2d方法繪制二維的直方圖:


hist2d是使用坐標軸正交的方塊分割區域,還有一種常用的方式是正六邊形也就是蜂窩形狀的分割。Matplotlib提供的plt.hexbin就是滿足這個需求的:
plt.hexbin(x,y,gridsize=30, cmap='Blues')
plt.colorbar(label='count in bin')
