matplotlib 使用www.example后,弹出窗口“Figure 1”为空 www.example.com ()绘制二元函数的3D图形

yzckvree  于 2023-05-01  发布在  其他
关注(0)|答案(1)|浏览(139)

我正在使用VScode绘制一个二元函数的3D图形。但是在运行代码之后,弹出的图形是空白的,并且不显示绘图。下面是代码:

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

figure = plt.figure()
ax = Axes3D(figure)
X = np.arange(-10, 10, 0.1)
Y = np.arange(-10, 10, 0.1)
X, Y = np.meshgrid(X, Y)
Z = (np.sin(X)*np.sin(Y))/(X*Y)  # f(x,y)=(sin(x)*sin(y))/(x*y)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='rainbow')
plt.show()

After running the code:
I have checked the setting我也试着画一个2D图像,它没有问题。

xj3cbfub

xj3cbfub1#

您不必导入Axes3D并将其示例化以创建3D打印。实际上,查看official documentation,建议的方法是从图中调用add_subplot(projection="3d")方法。具体操作如下:

from matplotlib import pyplot as plt
import numpy as np

figure = plt.figure()
ax = figure.add_subplot(projection="3d")
X = np.arange(-10, 10, 0.1)
Y = np.arange(-10, 10, 0.1)
X, Y = np.meshgrid(X, Y)
Z = (np.sin(X)*np.sin(Y))/(X*Y)  # f(x,y)=(sin(x)*sin(y))/(x*y)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='rainbow')
plt.show()

相关问题