[Numpy-discussion] loading and filtering data for matplotlib

josef.pktd at gmail.com josef.pktd at gmail.com
Tue Oct 26 11:21:15 EDT 2010


On Tue, Oct 26, 2010 at 10:57 AM, Resmi <l.resmi at gmail.com> wrote:
> Hi
>  I'm new to python. I'm trying to plot a data set in matplotlib.
>
>  The data is a mixture of strings and floats, has four columns
> (col[0],col[1],col[2],col[3]) and it looks like:
>
> R       -2.29350   0.50340   0.480E-01
> R       -2.25903   0.50740   0.480E-01
> SU      -2.19457   0.16200   0.800E-01
> SU      -2.13237   0.14600   0.800E-01
>
> What I want to do is to plot the SU filtered entries alone as col[1]:col[2]
>
> I wrote a script to read the data, how can I make x1 and SUx2 as
> arrays? Right now matplotlib only plots the last value obviously. Or
> is there a different/better way to slice just the SU entries and make
> them as a separate array? My actual data file has many more entries
> with more filters and I want to make a plot for each of the filters
> with col[1]:col[2]
>
> import pylab
> import numpy
> #Loading and reading data
> data=numpy.loadtxt('file.dat',dtype=[('filt','S4'),('x1','f8'),('x2','f8'),('x3','f8')])
> filt=data['filt']
> for i in range(0,len(filt),1):
>  if filt[i] == 'SU':
>  x1=data['x1'][i]
>  SUx2=data['x2'][i]

here you are overwriting you x1 each time you go through the loop and
it contains only one value

the vectorized version that should work (minus typos, just written not tested)

filt = data['filt']
mask = filt=='SU'
x1=data['x1'][mask]
SUx2 = data['x2'][mask]

no loop necessary.

Josef

> #print x1,SUx2
> pylab.plot(x1,SUx2,'bs')
> pylab.show()
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>



More information about the NumPy-Discussion mailing list