发布于 

3.6版本后的matplotlib使用plot_surface作图无效果的解决方法

在使用3.6.2版本的matplotlib使用以下代码进行作图时发现plot_surface无效果,显示的图上只有坐标,没有数据。

1
2
3
4
5
6
7
8
9
10
11
12
13
fig = plt.figure()
ax = Axes3D(fig)

X = np.linspace(-3, 3, 100)
Y = np.linspace(-3, 3, 100)
X, Y = np.meshgrid(X, Y)
Z = X * Y

# 绘制3D图形
surf = ax.plot_surface(X, Y, Z)
plt.title("A figure of 3D")
plt.show()

结果如图:

百度了一圈发现大家都是这样写的,并没有问题;又在官方文档里转了半天也没找到解决方法,遂将matplotlib的版本回退到3.5.1,再次运行上述代码,发现这次居然有效果了,并且提示了以下内容:

MatplotlibDeprecationWarning: Axes3D(fig) adding itself to the figure is deprecated since 3.4. Pass the keyword argument auto_add_to_figure=False and use fig.add_axes(ax) to suppress this warning. The default value of auto_add_to_figure will change to False in mpl3.5 and True values will no longer work in 3.6. This is consistent with other Axes classes.

这说明上述的用法在最新版的matplotlib中已被弃用,将

1
2
fig = plt.figure()
ax = Axes3D(fig)

改为

1
2
fig = plt.figure()
ax = fig.add_axes(Axes3D(fig))

就可以正常显示了。

END
关注我的公众号:嵌入式技术栈

本站由 @44 使用 Stellar 主题创建。
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处及作者。
本页面访问 次 | 总访客 人 | 共计访问 次 |

沪ICP备2023014561号-1