python hist2d未定义

1 投票
1 回答
1566 浏览
提问于 2025-04-18 07:22

我想用Python画一个二维直方图。我在matplotlib的维基上找到了下面这个例子。

from pylab import *
x = randn(1000)
y = randn(1000)+5

#normal distribution center at x=0 and y=5
hist2d(x,y,bins=40)
show()

但是我遇到了这个错误。

> Traceback (most recent call last):   File "./hist2d_demo.py", line 9,
> in <module>
>     hist2d(x,y,bins=40) NameError: name 'hist2d' is not defined

有没有人知道我该怎么解决这个问题?

1 个回答

1

我刚把你的代码复制粘贴到我的 Python 2.7.6 里,结果运行得非常好。 我还安装了 numpy 1.8.0、scipy 0.9.0 和 matplotlib 1.3.0。 你可能安装的 matplotlib 版本太旧了,可以这样检查一下:

import matplotlib
print matplotlib.__version__

撰写回答