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

发布于 2024-12-12 10:22:04 字数 759 浏览 1 评论 0原文

我在使用 Matplotlib 重新绘制一些 3D 数据时遇到了一个非常简单的问题。最初,我在画布上有一个带有 3D 投影的图形:

self.fig = plt.figure()
self.canvas = FigCanvas(self.mainPanel, -1, self.fig)
self.axes = self.fig.add_subplot(111, projection='3d')

在此处输入图像描述

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

在此处输入图像描述

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

I have what is probably a very simple problem replotting some 3D data using Matplotlib. Initially, I have an figure with a 3D projection on a canvas:

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

I then add some data and use canvas.draw() to update. The plot itself updates as expected, but I get additional 2D axis on the outside of the figure (-0.05 to 0.05) and I can't work out how to stop it:

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

Any ideas? I'm going in circles right now!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尘世孤行 2024-12-19 10:22:04

使用 mpl_toolkits.mplot3d.art3d.Patch3DCollection< 的 remove 方法代替 axes.clear() + fig.add_subplot /code> object:

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

Instead of axes.clear() + fig.add_subplot, use the remove method of the mpl_toolkits.mplot3d.art3d.Patch3DCollection object:

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

If you still have problems you can play with this:

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')
您的好友蓝忘机已上羡 2024-12-19 10:22:04

Joquin 的建议效果很好,并强调我可能一开始就以错误的方式进行策划。然而,为了完整起见,我最终发现您可以简单地通过使用来摆脱 2D 轴:

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

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

Joquin's suggestions worked well and highlighted that I was probably going about plotting the wrong way to start with. However, for the sake of completeness, I eventually found that you can get rid of the 2D axis simply by using:

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

This seems to be one way at least of removing the 2D labels from 3D plots if they appear.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文