我想做類似於http://matplotlib.org/examples/pylab_examples/hist2d_log_demo.html的東西,但我讀過使用pylab代碼而不是python交互模式是不好的做法,所以我想用matplotlib.pyplot來做到這一點。但是,我無法弄清楚如何使用pyplot使這個代碼工作。使用,pylab,給出的例子是如何在matplotlib.pyplot中使用帶有hist2d的colorbar?
from matplotlib.colors import LogNorm
from pylab import *
#normal distribution center at x=0 and y=5
x = randn(100000)
y = randn(100000)+5
hist2d(x, y, bins=40, norm=LogNorm())
colorbar()
show()
我已經嘗試了很多像
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
h1 = ax1.hist2d([1,2],[3,4])
,從這裏我從plt.colorbar(h1)
plt.colorbar(ax1)
plt.colorbar(fig)
ax.colorbar()
等等等等都試過了,我不能讓任何工作。
一般來說,即使在閱讀http://matplotlib.org/faq/usage_faq.html之後,我仍然不太清楚pylab和pyplot之間的關係。例如pylab中的show()
似乎在pyplot中變成plt.show()
,但由於某種原因colorbar
不會變成plt.colorbar()
?
例如,
其實'colorbar'葉matplotlib本身:[鏈接](http://matplotlib.org/api/colorbar_api.html)。所以你可能需要''將mpl'和'mpl.colorbar()'導入matplotlib。 – arbulgazar