## Re: [PyX-user] Drawing functions with discontinuities

 Re: [PyX-user] Drawing functions with discontinuities From: Andre Wobst - 2006-02-17 16:10:04 ```Hi, On 17.02.06, Arnd Baecker wrote: > I think you will have to split the plotting into parts, e.g., > if you have Numeric or numpy or numarray installed, you could do > > ################################################# > from pyx import * > from math import pi > from Numeric import arange, cos > > npts = 100 > x = pi/2.0*arange(npts)/(npts-1) > y = cos(x) > > g = graph.graphxy(width=8, height=3.25, xpos=0, ypos=8, x2=None, y2=None, > x=graph.axis.lin(min=0, max=5, title="\$t [s]\$"), > y=graph.axis.lin(min=-0.3, max=1.3, title="\$U(t) [V]\$")) > for i in xrange(4): > g.plot(graph.data.list(zip(x+i*pi/2.0, y), x=1, y=2), > [graph.style.line()]) > > g.writePDFfile("discont") > ################################################## > > Of course you can construct x and y as lists, but then > adding i*pi/2.0 won't work. Hey, that's pretty cool! I have a variant of that in case you don't want the vertial lines (as it would be mathematically correct). You can always insert "invalid" datapoints (everything that'll raise an error in an axis convert method). Those invalid points will lead to non-contiuous curves. In this case the y axis would be best to use, since you don't need the math on it anymore and thus can make a list from it (to enable different data types). Just do the following x = pi/2.0*arange(npts)/(npts-2) y = list(cos(x)) y[-1] = None instead of the "x = ..." and "y = ..." lines above. André -- by _ _ _ Dr. André Wobst / \ \ / ) wobsta@..., http://www.wobsta.de/ / _ \ \/\/ / PyX - High quality PostScript and PDF figures (_/ \_)_/\_/ with Python & TeX: visit http://pyx.sourceforge.net/ ```

 [PyX-user] Drawing functions with discontinuities From: Markus Meyer - 2006-02-17 14:37:32 ```Hi everyone, I'm trying to draw graphs with discontinuities in it, like this: from pyx import * from math import cos, pi def f(x): return cos(x % (pi/2)) g = graph.graphxy(width=8, height=3.25, xpos=0, ypos=8, x2=None, y2=None, x=graph.axis.lin(min=0, max=5, title="\$t [s]\$"), y=graph.axis.lin(min=-0.3, max=1.3, title="\$U(t) [V]\$")) g.plot(graph.data.function("y(x)=f(x)", context=locals()), [graph.style.line()]) g.writePDFfile("discont") However, there are two problems with this: 1. The vertical slopes between the single instances of the sawtooths are not perfectly vertical. In cases, where I want to explicitely visualize that a function has discontinuities, this is something that will probably make the reader stumble. 2. In most books, the vertical slopes are not displayed at all. I cannot currently do this with graph.data.function. When using a graph.data.paramfunction, it looks a bit better, but when zooming in, one can still see, that the slopes are not perfectly vertical. I know that I can use graph.data.list to exactly control the points that are output, but I guess there should be a better way? Markus ```
 Re: [PyX-user] Drawing functions with discontinuities From: Arnd Baecker - 2006-02-17 15:35:22 ```Hi, On Fri, 17 Feb 2006, Markus Meyer wrote: > Hi everyone, > > I'm trying to draw graphs with discontinuities in it, like this: > > from pyx import * > from math import cos, pi > > def f(x): > return cos(x % (pi/2)) > > g = graph.graphxy(width=8, height=3.25, xpos=0, ypos=8, x2=None, y2=None, > x=graph.axis.lin(min=0, max=5, title="\$t [s]\$"), > y=graph.axis.lin(min=-0.3, max=1.3, title="\$U(t) [V]\$")) > g.plot(graph.data.function("y(x)=f(x)", context=locals()), > [graph.style.line()]) > g.writePDFfile("discont") > > However, there are two problems with this: > > 1. The vertical slopes between the single instances of the sawtooths are > not perfectly vertical. In cases, where I want to explicitely visualize > that a function has discontinuities, this is something that will > probably make the reader stumble. > > 2. In most books, the vertical slopes are not displayed at all. I cannot > currently do this with graph.data.function. > > When using a graph.data.paramfunction, it looks a bit better, but when > zooming in, one can still see, that the slopes are not perfectly > vertical. I know that I can use graph.data.list to exactly control the > points that are output, but I guess there should be a better way? I think you will have to split the plotting into parts, e.g., if you have Numeric or numpy or numarray installed, you could do ################################################# from pyx import * from math import pi from Numeric import arange, cos npts = 100 x = pi/2.0*arange(npts)/(npts-1) y = cos(x) g = graph.graphxy(width=8, height=3.25, xpos=0, ypos=8, x2=None, y2=None, x=graph.axis.lin(min=0, max=5, title="\$t [s]\$"), y=graph.axis.lin(min=-0.3, max=1.3, title="\$U(t) [V]\$")) for i in xrange(4): g.plot(graph.data.list(zip(x+i*pi/2.0, y), x=1, y=2), [graph.style.line()]) g.writePDFfile("discont") ################################################## Of course you can construct x and y as lists, but then adding i*pi/2.0 won't work. Best, Arnd ```
 Re: [PyX-user] Drawing functions with discontinuities From: Andre Wobst - 2006-02-17 16:10:04 ```Hi, On 17.02.06, Arnd Baecker wrote: > I think you will have to split the plotting into parts, e.g., > if you have Numeric or numpy or numarray installed, you could do > > ################################################# > from pyx import * > from math import pi > from Numeric import arange, cos > > npts = 100 > x = pi/2.0*arange(npts)/(npts-1) > y = cos(x) > > g = graph.graphxy(width=8, height=3.25, xpos=0, ypos=8, x2=None, y2=None, > x=graph.axis.lin(min=0, max=5, title="\$t [s]\$"), > y=graph.axis.lin(min=-0.3, max=1.3, title="\$U(t) [V]\$")) > for i in xrange(4): > g.plot(graph.data.list(zip(x+i*pi/2.0, y), x=1, y=2), > [graph.style.line()]) > > g.writePDFfile("discont") > ################################################## > > Of course you can construct x and y as lists, but then > adding i*pi/2.0 won't work. Hey, that's pretty cool! I have a variant of that in case you don't want the vertial lines (as it would be mathematically correct). You can always insert "invalid" datapoints (everything that'll raise an error in an axis convert method). Those invalid points will lead to non-contiuous curves. In this case the y axis would be best to use, since you don't need the math on it anymore and thus can make a list from it (to enable different data types). Just do the following x = pi/2.0*arange(npts)/(npts-2) y = list(cos(x)) y[-1] = None instead of the "x = ..." and "y = ..." lines above. André -- by _ _ _ Dr. André Wobst / \ \ / ) wobsta@..., http://www.wobsta.de/ / _ \ \/\/ / PyX - High quality PostScript and PDF figures (_/ \_)_/\_/ with Python & TeX: visit http://pyx.sourceforge.net/ ```