2013-10-21 27 views
0

我使用它使用PolyCollection堆疊XY-地塊mplot3d例如matplotlib mplot3d情節出現奇數行的文物,http://matplotlib.org/examples/mplot3d/polys3d_demo.html使用PolyCollection

但是,我看到的情節有些奇怪線文物。

  • 如何刪除從可見區域消失的水平線?
  • 沿深度方向堆積XY圖有沒有更好的方法?

下面的腳本生成該地塊,

Odd lines in mplot3d plot

from mpl_toolkits.mplot3d import Axes3D 
from matplotlib.collections import PolyCollection 
from matplotlib.colors import colorConverter 
import matplotlib.pyplot as plt 
import numpy as np 

zs = [] 
fig = plt.figure() 
ax = fig.gca(projection='3d') 
verts = [] 

# XY data (i.e. "normal line plots") 
count = 4 
for i in range(count): 
    xs, ys = [800.0, 900.0, 1000.0, 1100.], [0., 1., 1., 0.] 
    verts.append(list(zip(xs, ys))) 

# Z position (i.e. depth at which the XY plot is drawn) 
zs = [0,1,2,3] 

colours = plt.cm.Blues(np.linspace(0.2, 0.8, len(zs))) 
poly = PolyCollection(verts, facecolors = colours) 
ax.add_collection3d(poly, zs=zs, zdir='y') 

ax.set_xlabel('X') 
ax.set_xlim3d(800,1150) 
ax.set_ylabel('Y') 
ax.set_ylim3d(0, 4) 
ax.set_zlabel('Z') 
ax.set_zlim3d(0, 1) 
plt.show() 
+0

這看起來像我的錯誤。您應該在matplotlib錯誤跟蹤器中添加一張票。 –

+0

我發佈的腳本是示例腳本的稍微修改版本,基本上只是更改了數據。爲什麼你認爲這個例子不起作用?肯定是一個錯誤? –

+0

當您更改x座標時,該示例還顯示該錯誤。 matplotlib似乎在(0,0)處插入一個額外的點到多邊形。這看起來像是一個bug。 –

回答

1

如果你仍然有這個問題,嘗試手動改變相應的路徑對象。你應該修改的最後頂點的代碼STOP(代碼0):

from mpl_toolkits.mplot3d import Axes3D 
from matplotlib.collections import PolyCollection 
from matplotlib.colors import colorConverter 
import matplotlib.pyplot as plt 
import numpy as np 

zs = [] 
fig = plt.figure() 
ax = fig.gca(projection='3d') 
verts = [] 

# XY data (i.e. "normal line plots") 
count = 4 
for i in range(count): 
    xs, ys = [800.0, 900.0, 1000.0, 1100.], [0., 1., 1., 0.] 
    verts.append(list(zip(xs, ys))) 

# Z position (i.e. depth at which the XY plot is drawn) 
zs = [0,1,2,3] 

colours = plt.cm.Blues(np.linspace(0.2, 0.8, len(zs))) 
poly = PolyCollection(verts, facecolors = colours) 

for path in poly.get_paths() : # There is the fix : 
    path.codes[-1] = 0  # we have to manually switch the last point in a path to STOP (code = 0) 

ax.add_collection3d(poly, zs=zs, zdir='y') 

ax.set_xlabel('X') 
ax.set_xlim3d(800,1150) 
ax.set_ylabel('Y') 
ax.set_ylim3d(0, 4) 
ax.set_zlabel('Z') 
ax.set_zlim3d(0, 1) 
plt.show() 

...這裏是結果: enter image description here

+0

這個工作,但你也可以做'PolyCollection(... closed = False)'作爲上面的@tcaswell評論。 –