2017-06-15 94 views
1

我想在Python中實現一個二維直方圖的colorbar。Colorbar二維直方圖Python

這裏是我的代碼:

import matplotlib.pyplot as plt 
import numpy as np 


mean=[0,0] 
cov=[[1,1],[1,2]] 
x,y = np.random.multivariate_normal(mean,cov,10000).T 

fig=plt.figure() 
ax=plt.axes() 

cax=ax.hist2d(x,y,bins=30,cmap="Blues") 
cb=fig.colorbar(cax) 
cb.ax.set_label("counts in bin") 

plt.show() 

但在這裏我得到的錯誤信息:

AttributeError: 'tuple' object has no attribute 'autoscale_None'

我在做什麼錯? 我想要這個面向對象,因此我想使用ax和fig的方法而不是使用plt的函數。

我希望有人能幫助我...

回答

4

ax.hist2d返回一個元組:

The return value is (counts, xedges, yedges, Image) .

你只需要爲你的彩條圖像:

cb=fig.colorbar(cax[3])