我有一个使用 Matplotlib 重新绘制一些 3D 数据的可能非常简单的问题。最初,我在 Canvas 上有一个带有 3D 投影的图形:
self.fig = plt.figure()
self.canvas = FigCanvas(self.mainPanel, -1, self.fig)
self.axes = self.fig.add_subplot(111, projection='3d')
然后我添加一些数据并使用 canvas.draw() 进行更新。该图本身按预期更新,但我在图的外部得到了额外的 2D 轴(-0.05 到 0.05),我不知道如何停止它:
self.axes.clear()
self.axes = self.fig.add_subplot(111, projection='3d')
xs = np.random.random_sample(100)
ys = np.random.random_sample(100)
zs = np.random.random_sample(100)
self.axes.scatter(xs, ys, zs, c='r', marker='o')
self.canvas.draw()
有什么想法吗?我现在要兜圈子了!
最佳答案
使用 mpl_toolkits.mplot3d.art3d 的
对象:remove
方法代替 axes.clear()
+ fig.add_subplot
.Patch3DCollection
In [31]: fig = plt.figure()
In [32]: ax = fig.add_subplot(111, projection='3d')
In [33]: xs = np.random.random_sample(100)
In [34]: ys = np.random.random_sample(100)
In [35]: zs = np.random.random_sample(100)
In [36]: a = ax.scatter(xs, ys, zs, c='r', marker='o') #draws
In [37]: a.remove() #clean
In [38]: a = ax.scatter(xs, ys, zs, c='r', marker='o') #draws again
如果你还有问题,你可以试试这个:
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import interactive
interactive(True)
xs = np.random.random_sample(100)
ys = np.random.random_sample(100)
zs = np.random.random_sample(100)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
a = ax.scatter(xs, ys, zs, c='r', marker='o')
plt.draw()
raw_input('press for new image')
a.remove()
xs = np.random.random_sample(1000)
ys = np.random.random_sample(1000)
zs = np.random.random_sample(1000)
a = ax.scatter(xs, ys, zs, c='r', marker='o')
plt.draw()
raw_input('press to end')
关于python - Matplotlib:使用 canvas.draw() 重新绘制 3D 图形时的附加轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7947816/