我是编程新手,我不明白为什么我导入的模块from mpl_toolkits.mplot3d import Axes3D
在PyCharm
中显示为灰色。如果我没有它运行程序,我会收到错误。我确定我在这个项目中所做的事情有很多问题。我只是试图获得特定函数和曲面图的等高线图。任何建议将不胜感激。
from mpl_toolkits.mplot3d import Axes3D <---- Grey
import numpy as np
from numpy import arange
from pylab import meshgrid, imshow, contour, colorbar, show
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
class Graphs(object):
def z_func(self, x, y):
return -x*y*(np.exp(-(np.square(x))-(np.square(y))))
def contour_graph(self, x_c, y_c):
x_two, y_two = meshgrid(x_c, y_c)
z = self.z_func(x_two, y_two)
im = imshow(z, cmap=cm.RdBu)
contour(z, arange(-1, 1.5, 0.01), linewidths=1, cmap=cm.Set3)
colorbar(im)
show()
def surface_plot(self, x_s, y_s):
x_s2, y_s2 = meshgrid(x_s, y_s)
z_s = self.z_func(x_s2, y_s2)
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(x_s2, y_s2, z_s, rstride=1, cstride=1,
cmap=cm.RdBu, linewidth=0, antialiased=False)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
g = Graphs()
x_func = arange(-3.0, 3.0, 0.01)
y_func = arange(-3.0, 3.0, 0.01)
#c = g.contour_graph(x_func, y_func)
g.surface_plot(x_func, y_func)
这些是我拿出模块时遇到的错误:
追踪(最近一次通话): 文件&#34; C:\ Users \ cwoyt \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ matplotlib \ projections__init __。py&#34;,第65行,在get_projection_class中 return projection_registry.get_projection_class(投影) 文件&#34; C:\ Users \ cwoyt \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ matplotlib \ projections__init __。py&#34;,第29行,在get_projection_class中 return self._all_projection_types [name] KeyError:&#39; 3d&#39; 在处理上述异常期间,发生了另一个异常:
追踪(最近一次通话): File&#34; C:/Users/cwoyt/PycharmProjects/graph/graph_2.py" ;,第44行,在 g.surface_plot(x_func,y_func) 文件&#34; C:/Users/cwoyt/PycharmProjects/graph/graph_2.py",第29行,在surface_plot中 ax = fig.gca(projection =&#39; 3d&#39;) 文件&#34; C:\ Users \ cwoyt \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ matplotlib \ figure.py&#34;,第1368行,在gca中 return self.add_subplot(1,1,1,** kwargs) 文件&#34; C:\ Users \ cwoyt \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ matplotlib \ figure.py&#34;,第1002行,在add_subplot中 自我,* args,** kwargs) 文件&#34; C:\ Users \ cwoyt \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ matplotlib \ projections__init __。py&#34;,第98行,在process_projection_requirements中 projection_class = get_projection_class(投影) 文件&#34; C:\ Users \ cwoyt \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ matplotlib \ projections__init __。py&#34;,第67行,get_projection_class 引发ValueError(&#34;未知预测&#39;%s&#39;&#34;%投影) ValueError:未知投影&#39; 3d&#39;
现在这个程序是文件夹中唯一的一个。