2011-10-30 94 views
4

我有什麼可能是使用Matplotlib重新繪製某些3D數據的一個非常簡單的問題。最初,我有一個畫布上的3D投影的圖:Matplotlib:當使用canvas.draw重繪圖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()來更新。劇情本身更新符合市場預期,但我得到的數字(-0.05〜0.05)之外的其他2D軸,我無法工作,如何阻止它:

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

任何想法?我現在正在圈子裏!

回答

2

Joquin的建議行之有效,並強調我可能會開始繪製錯誤的方式。然而,爲了完整起見,我終於發現,你可以擺脫2D軸只需通過使用:

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

這似乎是至少的,如果他們出現從3D繪圖移除2D標籤的一種方式。

3

而不是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 

如果仍然有問題,你可以玩這個:

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

嗨華金, 感謝您花時間回覆。我試過這個,但它似乎沒有工作。 remove()似乎清除三維散點數據,但對軸不起作用。此外,2D軸(圖中的外側-0.05至0.05)仍然存在。任何其他想法? – Dan

+0

2D軸的標籤不應該在第一位。你是否從你的代碼中移除了axes.clear()行? – joaquin

+0

你好, 是的,我做到了。第一次繪製時,2D軸不存在。但第二次,他們再次出現。代碼是: xs = np.random.random_sample(100)* 40 - 20 ys = np.random.random_sample(100)* 40 - 20 zs = np.random.random_sample(100)* 40 - 20 a = self.axes.scatter(xs,ys,zs,c ='r',marker ='o') a.remove() a = self.axes.scatter(xs,ys,zs,c ='r ',marker ='o') self.canvas.draw() – Dan