简体   繁体   English

在numpy上绘制2D高斯

[英]Plot a 2D gaussian on numpy

I am a new in numpy and I try to practice the basic operations. 我是numpy的新手,我尝试练习基本操作。 Here is a code to plot a 2D gaussian distribution. 这是绘制2D高斯分布的代码。 I have an error in matplotlib. 我在matplotlib中出错。 How can I fix that? 我该如何解决?

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

X,Y=np.meshgrid(np.linspace(-1,1,10),np.linspace(-1,1,10));
mu,sigma=0,1; #suppose that mux=muy=mu=0 and sigmax=sigmay=sigma
G=np.exp(-((X-mu)**2+(Y-mu)**2)/2.0*sigma**2)
print G
fig=plt.figure();
ax=fig.add_subplot(111,projection='3d')
surf=ax.plot_surface(X,Y,G,c='red')
plt.show()

I have this error 我有这个错误
File "/Library/Python/2.7/site-packages/mpl_toolkits/mplot3d/__init__.py", line 4, in from matplotlib.externals import six ImportError: No module named externals 来自matplotlib.externals的文件“ /Library/Python/2.7/site-packages/mpl_toolkits/mplot3d/__init__.py”,第4行,导入六个ImportError:没有名为外部的模块

Because with my versions it works so I suspect you need to upgrade mpl_toolkits (and/or matplotlib ). 因为使用我的版本可以工作,所以我怀疑您需要升级mpl_toolkits (和/或matplotlib )。

It seems like matplotlib dropped support for older Python versions (and you have a more recent version) and therefore doesn't need an externals submodule anymore. 看起来matplotlib放弃了对旧版本Python的支持(并且您拥有更新的版本),因此不再需要externals子模块。 But you didn't update mpl_toolkits which still thinks matplotlib has this submodule. 但是您没有更新mpl_toolkits ,后者仍然认为matplotlib具有此子模块。 And therefore you get this Error. 因此,您会收到此错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM