python - Matplotlib:使用 canvas.draw() 重新绘制 3D 图形时的附加轴

标签 python matplotlib

我有一个使用 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')

enter image description here

然后我添加一些数据并使用 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()

enter image description here

有什么想法吗?我现在要兜圈子了!

最佳答案

使用 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/

相关文章:

python - "Fatal error in launcher: Unable to create process using"是什么意思?

python - 如何扫描最新的 TRX 区 block 以获取转账事件以检查地址是否有新交易

Python 请求 - 快速知道响应是否是 json 可解析的

python - 在 C++ 中初始化非常大的 vector

python - 绘制直方图 : TypeError: cannot perform reduce with flexible type

matplotlib - Quiver 2D 颜色图

python - Selenium - 引发 httplib.BadStatusLine

python - matplotlib 饼图的多个图例?

python - Python 中 4D 向量的可视化

python - matplotlib.pyplot 和 matplotlib.figure 有什么区别?