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

zlhcx6iw  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(115)

我有一个可能是一个非常简单的问题,使用Matplotlib重新绘制一些3D数据。最初,我在画布上有一个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()

有什么办法吗?我现在在兜圈子!

ztyzrc3y

ztyzrc3y1#

使用mpl_toolkits.mplot3d.art3d.Patch3DCollection对象的remove方法,而不是axes.clear() + fig.add_subplot

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')
f45qwnt8

f45qwnt82#

Joquin的建议很有效,并强调了我可能会以错误的方式开始绘制。然而,为了完整起见,我最终发现您可以通过使用以下命令来摆脱2D轴:

self.axes.get_xaxis().set_visible(False)
self.axes.get_yaxis().set_visible(False)

这似乎至少是一种从3D图中删除2D标签的方法,如果它们出现的话。

相关问题