[SciPy-User] pylab

Benjamin Root ben.root@ou....
Mon Jul 19 18:59:29 CDT 2010


On Mon, Jul 19, 2010 at 6:49 PM, Joshua Holbrook <josh.holbrook@gmail.com>wrote:

> On Mon, Jul 19, 2010 at 3:39 PM,  <PHobson@geosyntec.com> wrote:
> >
> >
> >> -----Original Message-----
> >> From: scipy-user-bounces@scipy.org [mailto:scipy-user-bounces@scipy.org
> ]
> >> On Behalf Of Joshua Holbrook
> >> Sent: Monday, July 19, 2010 4:24 PM
> >> To: SciPy Users List
> >> Subject: Re: [SciPy-User] pylab
> >>
> >> 2010/7/19 பழநி சே <palaniappan.chetty@gmail.com>:
> >> > hi,
> >> > I have a question about pylab/matplotlib, I am interested in plots and
> >> > I want to know if I can have some data points in a data sets missing
> >> > but still create a plot using pylab? For example (assuming all modules
> >> > have been imported)
> >> >
> >> >>x = [1,2,3,4]
> >> >>y=[10,20,30,40]
> >> >>pylab.plot(x,y)
> >> >>pylab.show()
> >> >
> >> > works fine. But what if I have one or more data points missing in my y
> >> > data set? like this
> >> >
> >> >>x = [1,2,3,4]
> >> >>y=[10,20, ,40]
> >> >>pylab.plot(x,y)
> >> >>pylab.show()
> >> > I know that I cannot have an empty element in my list and this does
> not
> >> work
> >> >
> >> > Thanks
> >> > --
> >> > Palani
> >> >
> >>
> >> Hey Palani,
> >>
> >> I'm not extremely familiar with matplotlib, but my experience tells me
> >> that, while MPL itself wouldn't really have any nice way to do this,
> >> that you could import a dataset and use python/numpy to clean it up.
> >> For example, you could maybe use filter() and zip()  (zip's my
> >> favorite toy), maybe like this:
> >>
> >>
> >>     In [30]: x
> >>     Out[30]: [0, 1, 2, 3, 4]
> >>
> >>     In [31]: y
> >>     Out[31]: [0, 1, 4, None, 16]
> >>
> >>     In [32]: zip(*filter(lambda x: x[1] != None, zip(x,y)))
> >>     Out[32]: [(0, 1, 2, 4), (0, 1, 4, 16)]
> >>
> >> and then you could do plot(_[0],_[1]).  Alternately, and this would
> >> probably be worth investigating for bigger datasets, you could maybe
> >> use masked arrays
> >> (http://docs.scipy.org/doc/numpy/reference/maskedarray.baseclass.html)
> >> to do something similar in spirit.
> >>
> >> Hope that helped!
> >
> > As a big MPL user, that's an interesting solution. MPL and numpy were my
> primary gateways into Python from Matlab, so that's pretty informative for
> me. Given my background, I tend to take the more brute-force approach and
> would use the masked arrays.
> >
> > For the OP:
> > #---
> > import numpy as np
> > import matplotlib.pyplot as plt
> > x = np.arange(5)
> > y = np.ma.MaskedArray(data=[0,1,4,None,16],mask=[0,0,0,1,0])
> > fig = plt.figure()
> > ax1 = fig.add_subplot(111)
> > ax1.plot(x,y,'ko')
> > fig.savefig('masktest.png')
> >
> > -paul
> >
> > _______________________________________________
> > SciPy-User mailing list
> > SciPy-User@scipy.org
> > http://mail.scipy.org/mailman/listinfo/scipy-user
> >
> >
>
> So you can pass MaskedArrays to MPL and it'll filter them out on its
> own? Neat! Probably faster too, for large datasets.
>
>
>
--Josh
>

Yes, MaskedArrays are the preferred way to do this.  If you run into a
situation where plotting MaskedArrays does not work, then that is a bug and
should be reported.

Btw, I would avoid using None as an empty value.  NaNs might be better.

Ben Root
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20100719/13d55202/attachment.html 


More information about the SciPy-User mailing list