“matplotlib使用Axes3D绘3D图像时,没有内容”的解决方法

问题描述

今天在使用matplotlib绘3D图像时,运行图像内容竟然显示为空:

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

x1 = np.array([
    137.97, 104.50, 100.00, 124.32, 79.20, 99.00, 124.00, 114.00, 106.69,
    138.05, 53.75, 46.91, 68.00, 63.02, 81.26, 86.21
])
x2 = np.array([3, 2, 2, 3, 1, 2, 3, 2, 2, 3, 1, 1, 1, 1, 2, 2])
y = np.array([
    145.00, 110.00, 93.00, 116.00, 65.32, 104.00, 118.00, 91.00, 62.00, 133.00,
    51.00, 45.00, 78.50, 69.65, 75.69, 95.30
])

fig = plt.figure()

ax3d = Axes3D(fig)

ax3d.set_xlabel("square")
ax3d.set_ylabel("rooms")
ax3d.set_zlabel("price")
ax3d.scatter3D(x1, x2, y, c='r', cmap="Greens")
plt.show()


运行结果:
“matplotlib使用Axes3D绘3D图像时,没有内容”的解决方法_第1张图片

原因分析:

很奇怪的事,在同学电脑运行我的程序完全没有问题,因此我仔细想了一下。应该也是由于版本的问题,至此我仍未解决问题,我又询问了师兄。师兄给我提供了另外一种思路。也就是说,老师的课程录制的比较久远,而我是版本又比较新,所以给了我一种新的编写方案。

#ax3d = Axes3D(fig)  #原代码
ax3d = fig.add_axes(Axes3D(fig)) #新代码

“matplotlib使用Axes3D绘3D图像时,没有内容”的解决方法_第2张图片
至此,问题解决

你可能感兴趣的:(神经网络与深度学习,matplotlib,3d,python)