使用matplotlib Polycollection从csv文件绘制数据 [英] Using matplotlib Polycollection to plot data from csv files
问题描述
我一直在试图使用PolyCollection非常类似于这一个图:
http://matplotlib.org/examples/mplot3d/polys3d_demo.html
不同之处在于,我希望我的代码从文件夹并绘制数据(不是随机数)。每个CSV文件包含一个光谱,即两列和一定数量的行。我想要第一行是我的x值,第二行是相应的z值。
我试过这样:
import os
import numpy as np
import matplotlib.pyplot as plt
来自matplotlib.collections import PolyCollection
fig = plt.figure()
ax = fig.gca(projection ='3d')
input_path ='input'
spectrumfiles = []
对于root,dirs,os.walk中的文件(input_path):
文件中的名称:
如果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))
在谱文件中的名称:
spectrum = np.loadtxt(name,delimiter =',')
# xs = spectrum [:,0]#我试过这个,但后来意识到频谱
#ys = spectrum [:,1] #already有所需的形状
#verts.append(list (xs,ys)))
verts.append(spectrum)
poly = PolyCollection(verts,facecolors = np.ones (verts)))#我不用太费心了
poly.set_alpha(0.7)#colours在这一刻
ax.add_collection3d(poly,zs = zs,zdir ='y')
plt.show()
现在,我得到的图是空的。 / p>
编辑:
这里有4个示例文件
EDIT2:
这里有一些链接到stackoverflow的相关问题。
- Matplotlib在3d中绘制脉搏传播
- 瀑布图python?
好的,很容易解决 - 你唯一的问题是,因为你自己添加了PolyCollection到你的轴,它不会自动缩放x / y / z限制到适当的值(仍然显示0和1 )。对于您拥有的数据,如果添加以下行:
ax.set_xlim(0,5000)
.set_ylim(0,len(spectrumfiles))
那么你的数据会神奇地出现。
>
I've been trying to make a diagram using PolyCollection very similar to this one: http://matplotlib.org/examples/mplot3d/polys3d_demo.html
The difference is, I want my code to read all CSV files from a folder and plot the data in them (not random numbers). Each CSV file contains a spectrum, i.e. two columns and a certain number of rows. I want the first row to be my x-values and the second row the corresponding z-values.
I tried this:
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()
Now, the diagram I get is empty.
EDIT: here are 4 sample FILES
EDIT2: here are some links to related questions on stackoverflow. - Matplotlib plot pulse propagation in 3d - Waterfall plot python?
Okay, easy fix - your only problem is that because you added the PolyCollection to your axes yourself, it doesn't automatically scale the x/y/z limits to appropriate values (still shows between 0 and 1). For the data you have, if you add the following lines:
ax.set_xlim(0,5000)
ax.set_ylim(0,len(spectrumfiles))
then your data will magically appear.
这篇关于使用matplotlib Polycollection从csv文件绘制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!