1

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?

Community
  • 1
  • 1
Menardi
  • 25
  • 1
  • 7
  • just check this http://stackoverflow.com/questions/4622057/plotting-3d-polygons-in-python-matplotlib I am clueless – Kirubaharan J Dec 03 '14 at 15:51
  • there seem to be a couple of questions on here about PolyCollection. But I'm clueless, too. I'm adding them into my original question. – Menardi Dec 03 '14 at 16:55
  • What is the shape of `verts`? Also, does your data have any NaNs or other bad data in it? That would mess things up. – Ajean Dec 03 '14 at 17:20
  • shape(verts) returns (4L, 3449L, 2L), so that's 4 spectra with 3499 rows and 2 columns each. There are no NaNs in there and plotting each spectrum individually with matplotlib.pyplot.plot works just fine. – Menardi Dec 03 '14 at 18:04
  • When I took your code and replaced the file reads with random numbers (e.g. `spectrum = np.random.rand(100,2)`) it worked fine. I'll see what your reads do. – Ajean Dec 03 '14 at 22:08

1 Answers1

1

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.

data visible now

Ajean
  • 5,528
  • 14
  • 46
  • 69