2014-12-03 168 views
1

我一直試圖讓使用PolyCollection非常相似的圖這一個: http://matplotlib.org/examples/mplot3d/polys3d_demo.html使用matplotlib Polycollection從CSV文件繪製數據

不同的是,我希望我的代碼來讀取從所有CSV文件文件夾並繪製數據(不是隨機數)。每個CSV文件都包含一個光譜,即兩列和一定數量的行。我希望第一行是我的x值,第二行是相應的z值。

我嘗試這樣做:

import os 
import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib.collections import PolyCollection 

fig=plt.figure() 
ax = fig.gca(projection='3d') 

input_path = 'input'   
spectrumfiles = [] 
verts=[] 

for root, dirs, files in os.walk(input_path):  
    for name in files: 
     if os.path.splitext(name)[1] == '.CSV' or os.path.splitext(name)[1] == '.csv': 
      spectrumfiles.append(os.path.join(root,name)) 

zs = np.arange(len(files))        

for name in spectrumfiles: 
    spectrum = np.loadtxt(name, delimiter=',') 
    #xs = spectrum[:,0]     #I tried this first but then realised that "spectrum 
    #ys = spectrum[:,1]     #already has the needed shape    
    #verts.append(list(zip(xs,ys))) 

    verts.append(spectrum) 



poly = PolyCollection(verts, facecolors=np.ones(len(verts)))  #I'm not too bothered with 
poly.set_alpha(0.7)            #colours at the moment 
ax.add_collection3d(poly, zs=zs, zdir='y') 

plt.show() 

現在,圖我得到的是空的。

編輯: 這裏有4個樣品FILES

EDIT2: 這裏有一些鏈接到計算器相關問題。 - Matplotlib plot pulse propagation in 3d - Waterfall plot python?

+0

只是檢查這個http://stackoverflow.com/questions/4622057/plotting-3d-polygons-in-python-matplotlib我無言以對 – 2014-12-03 15:51:02

+0

似乎有成爲一對夫婦的問題,在這裏大約PolyCollection 。但我也很無能。我將它們添加到我原來的問題中。 – Menardi 2014-12-03 16:55:18

+0

'verts'的形狀是什麼?另外,您的數據是否有NaN或其他不良數據?這會搞砸了。 – Ajean 2014-12-03 17:20:15

回答

1

好了,簡單的辦法 - 你唯一的問題是,由於添加的PolyCollection到您自己的軸,它不會自動縮放X/Y/Z限制爲適當的值(仍顯示之間0和1)。對於您所擁有的數據,如果添加以下行:

ax.set_xlim(0,5000) 
ax.set_ylim(0,len(spectrumfiles)) 

然後您的數據將奇蹟般地出現。

data visible now

+0

非常感謝你!確實很簡單! – Menardi 2014-12-04 08:49:02