使用matplotlib Polycollection从csv文件打印数据

2024-10-01 17:24:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试使用PolyCollection绘制一个与此非常相似的图表: http://matplotlib.org/examples/mplot3d/polys3d_demo.html

不同的是,我希望我的代码从一个文件夹中读取所有的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

编辑2: 下面是一些与stackoverflow相关问题的链接。 -Matplotlib plot pulse propagation in 3d -Waterfall plot python?


Tags: csvpathnameinimportforinputmatplotlib
1条回答
网友
1楼 · 发布于 2024-10-01 17:24:59

好的,简单的解决方法-你唯一的问题是,因为你自己在你的轴上添加了多边形,它不会自动将x/y/z限制缩放到适当的值(仍然显示在0和1之间)。对于您拥有的数据,如果您添加以下行:

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

然后你的数据就会神奇地出现。在

data visible now

相关问题 更多 >

    热门问题