From: Xavier Gnata <xavier.gnata@gm...>  20091011 23:10:20

Hi, Imagine you have something like: from pylab import * t = arange(0.0, 2.0, 0.01) s = sin(2*pi*t) ax = subplot(111) ax.plot(t, s) That's fine but now I would like to plot the negative parts of the curve in red and the positive one in green. Is there a nice pylab oriented way to do that? Some kind of "conditional formating"? Xavier 
From: Eric Firing <efiring@ha...>  20091011 23:49:26

Xavier Gnata wrote: > Hi, > > Imagine you have something like: > > from pylab import * > t = arange(0.0, 2.0, 0.01) > s = sin(2*pi*t) > ax = subplot(111) > ax.plot(t, s) > > That's fine but now I would like to plot the negative parts of the curve > in red and the positive one in green. > Is there a nice pylab oriented way to do that? Some kind of "conditional > formating"? Not built in, but you can do it easily with masked arrays. See http://matplotlib.sourceforge.net/examples/pylab_examples/masked_demo.html It is not exactly what you want, but close: sneg = np.ma.masked_greater_equal(s, 0) spos = np.ma.masked_less_equal(s, 0) ax.plot(t, spos, 'g') ax.plot(t, sneg, 'r') What this does not do is ensure that there is no gap where the line crosses zero. For that, you would need to ensure that your sampling of s(t) includes the zeros. Eric > > Xavier > >  > Come build with us! The BlackBerry(R) Developer Conference in SF, CA > is the only developer event you need to attend this year. Jumpstart your > developing skills, take BlackBerry mobile applications to market and stay > ahead of the curve. Join us from November 9  12, 2009. Register now! > http://p.sf.net/sfu/devconference > _______________________________________________ > Matplotlibusers mailing list > Matplotlibusers@... > https://lists.sourceforge.net/lists/listinfo/matplotlibusers 
From: Xavier Gnata <xavier.gnata@gm...>  20091012 06:12:02

Eric Firing wrote: > Xavier Gnata wrote: >> Hi, >> >> Imagine you have something like: >> >> from pylab import * >> t = arange(0.0, 2.0, 0.01) >> s = sin(2*pi*t) >> ax = subplot(111) >> ax.plot(t, s) >> >> That's fine but now I would like to plot the negative parts of the >> curve in red and the positive one in green. >> Is there a nice pylab oriented way to do that? Some kind of >> "conditional formating"? > > Not built in, but you can do it easily with masked arrays. See > http://matplotlib.sourceforge.net/examples/pylab_examples/masked_demo.html > > > It is not exactly what you want, but close: > > sneg = np.ma.masked_greater_equal(s, 0) > spos = np.ma.masked_less_equal(s, 0) > ax.plot(t, spos, 'g') > ax.plot(t, sneg, 'r') > > What this does not do is ensure that there is no gap where the line > crosses zero. For that, you would need to ensure that your sampling > of s(t) includes the zeros. > > Eric It should do the trick because my sampling is very high. Xavier 
From: thkoe002 <thkoe002@gm...>  20091012 16:34:44

Maybe a little shorter is the where() keyword, and even that can be omitted: ax.plot(t[where(s>=0)],s[where(s>=0)],"g") ax.plot(t[where(s<0)],s[where(s<0)],"r") or, shorter: ax.plot(t[s>=0],s[s>=0],"g") ax.plot(t[s<0],s[s<0],"r") cheers Thomas Xavier Gnata2 wrote: > > Hi, > > Imagine you have something like: > > from pylab import * > t = arange(0.0, 2.0, 0.01) > s = sin(2*pi*t) > ax = subplot(111) > ax.plot(t, s) > > That's fine but now I would like to plot the negative parts of the curve > in red and the positive one in green. > Is there a nice pylab oriented way to do that? Some kind of "conditional > formating"? > > Xavier > >  > Come build with us! The BlackBerry(R) Developer Conference in SF, CA > is the only developer event you need to attend this year. Jumpstart your > developing skills, take BlackBerry mobile applications to market and stay > ahead of the curve. Join us from November 9  12, 2009. Register now! > http://p.sf.net/sfu/devconference > _______________________________________________ > Matplotlibusers mailing list > Matplotlibusers@... > https://lists.sourceforge.net/lists/listinfo/matplotlibusers > >  View this message in context: http://www.nabble.com/plotcolorasafunctionofvaluestp25848622p25858967.html Sent from the matplotlib  users mailing list archive at Nabble.com. 
From: Xavier Gnata <xavier.gnata@gm...>  20091012 18:40:20

ax.plot(t[s>=0],s[s>=0],"g") ax.plot(t[s<0],s[s<0],"r") Whaou! That's what I call a nice pythonic syntax. XAvier > Maybe a little shorter is the where() keyword, and even that can be omitted: > > ax.plot(t[where(s>=0)],s[where(s>=0)],"g") > ax.plot(t[where(s<0)],s[where(s<0)],"r") > > or, shorter: > > ax.plot(t[s>=0],s[s>=0],"g") > ax.plot(t[s<0],s[s<0],"r") > > cheers > > Thomas > > > > Xavier Gnata2 wrote: > >> Hi, >> >> Imagine you have something like: >> >> from pylab import * >> t = arange(0.0, 2.0, 0.01) >> s = sin(2*pi*t) >> ax = subplot(111) >> ax.plot(t, s) >> >> That's fine but now I would like to plot the negative parts of the curve >> in red and the positive one in green. >> Is there a nice pylab oriented way to do that? Some kind of "conditional >> formating"? >> >> Xavier >> >>  >> Come build with us! The BlackBerry(R) Developer Conference in SF, CA >> is the only developer event you need to attend this year. Jumpstart your >> developing skills, take BlackBerry mobile applications to market and stay >> ahead of the curve. Join us from November 9  12, 2009. Register now! >> http://p.sf.net/sfu/devconference >> _______________________________________________ >> Matplotlibusers mailing list >> Matplotlibusers@... >> https://lists.sourceforge.net/lists/listinfo/matplotlibusers >> >> >> > > 
From: Eric Firing <efiring@ha...>  20091012 19:50:42

Xavier Gnata wrote: > ax.plot(t[s>=0],s[s>=0],"g") > ax.plot(t[s<0],s[s<0],"r") > I don't think it does what you want, though, unless you are plotting markers, not lines. With lines, you will have line segments approximately on the xaxis across the gaps. Using masked arrays avoids that. The gaps (masked segments) will not be stroked. Eric > > Whaou! That's what I call a nice pythonic syntax. > > XAvier > >> Maybe a little shorter is the where() keyword, and even that can be omitted: >> >> ax.plot(t[where(s>=0)],s[where(s>=0)],"g") >> ax.plot(t[where(s<0)],s[where(s<0)],"r") >> >> or, shorter: >> >> ax.plot(t[s>=0],s[s>=0],"g") >> ax.plot(t[s<0],s[s<0],"r") >> >> cheers >> >> Thomas >> >> >> >> Xavier Gnata2 wrote: >> >>> Hi, >>> >>> Imagine you have something like: >>> >>> from pylab import * >>> t = arange(0.0, 2.0, 0.01) >>> s = sin(2*pi*t) >>> ax = subplot(111) >>> ax.plot(t, s) >>> >>> That's fine but now I would like to plot the negative parts of the curve >>> in red and the positive one in green. >>> Is there a nice pylab oriented way to do that? Some kind of "conditional >>> formating"? >>> >>> Xavier >>> >>>  >>> Come build with us! The BlackBerry(R) Developer Conference in SF, CA >>> is the only developer event you need to attend this year. Jumpstart your >>> developing skills, take BlackBerry mobile applications to market and stay >>> ahead of the curve. Join us from November 9  12, 2009. Register now! >>> http://p.sf.net/sfu/devconference >>> _______________________________________________ >>> Matplotlibusers mailing list >>> Matplotlibusers@... >>> https://lists.sourceforge.net/lists/listinfo/matplotlibusers >>> >>> >>> >> > > >  > Come build with us! The BlackBerry(R) Developer Conference in SF, CA > is the only developer event you need to attend this year. Jumpstart your > developing skills, take BlackBerry mobile applications to market and stay > ahead of the curve. Join us from November 9  12, 2009. Register now! > http://p.sf.net/sfu/devconference > _______________________________________________ > Matplotlibusers mailing list > Matplotlibusers@... > https://lists.sourceforge.net/lists/listinfo/matplotlibusers 