[英]Matplotlib: Additional axis when replotting 3D figure using canvas.draw()
I have what is probably a very simple problem replotting some 3D data using Matplotlib. 我有一个非常简单的问题,可能是使用Matplotlib重新绘制一些3D数据。 Initially, I have an figure with a 3D projection on a canvas: 最初,我在画布上有一个3D投影图:
self.fig = plt.figure()
self.canvas = FigCanvas(self.mainPanel, -1, self.fig)
self.axes = self.fig.add_subplot(111, projection='3d')
I then add some data and use canvas.draw() to update. 然后我添加一些数据并使用canvas.draw()进行更新。 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: 情节本身按预期更新,但我在图的外部得到了额外的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()
Any ideas? 有任何想法吗? I'm going in circles right now! 我现在就去圈子了!
Instead of axes.clear()
+ fig.add_subplot
, use the remove
method of the mpl_toolkits.mplot3d.art3d.Patch3DCollection
object: 而不是axes.clear()
+ fig.add_subplot
,使用mpl_toolkits.mplot3d.art3d.Patch3DCollection
对象的remove
方法:
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')
Joquin's suggestions worked well and highlighted that I was probably going about plotting the wrong way to start with. Joquin的建议运作良好,并强调我可能会以错误的方式开始策划。 However, for the sake of completeness, I eventually found that you can get rid of the 2D axis simply by using: 但是,为了完整起见,我最终发现只需使用以下方法就可以摆脱2D轴:
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. 这似乎是至少从3D图中删除2D标签的一种方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.