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

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

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中已被弃用,将

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

改为

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

就可以正常显示了。

  • 15
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值