[SciPy-User] pylab

PHobson at Geosyntec.com PHobson at Geosyntec.com
Mon Jul 19 19:39:18 EDT 2010



> -----Original Message-----
> From: scipy-user-bounces at scipy.org [mailto:scipy-user-bounces at 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 at 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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: masktest.png
Type: image/png
Size: 38523 bytes
Desc: masktest.png
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20100719/10983277/attachment.png>


More information about the SciPy-User mailing list