From: Xavier G. <xav...@gm...> - 2009-10-12 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 |