Discussion:
[PyX-user] Plotting Piecewise Functions
Dan Reinholz
2008-06-10 19:15:14 UTC
Permalink
Hopefully I won't become a nuisance in my quest to
become adept with PyX. : )

My question this time is what is the preferred method
to plot a piecewise function?

For instance, the function I want to plot at the
moment is:

$$
f(x) = \left\{
\begin{array}{ll}
x & 0 \leq x < 1 \\
0 & x = 1 \\
1 & 1 < x \leq 2 \\
3 - x & 2 < x < 3 \\
1 & 3 \leq x \leq 4, x \neq 3.5 \\
2 & x = 3.5 \\
\end{array}\right.
$$

but a much simpler example/explanation would be more
than enough for me.

Thanks again,
-Daniel
Alan G Isaac
2008-06-10 19:53:38 UTC
Permalink
Post by Dan Reinholz
My question this time is what is the preferred method
to plot a piecewise function?
No claim of expertise, but ...
Generate the data, and plot each piece separately.
(Duplicating endpoints.)

hth,
Alan Isaac
Stefan Schenk
2008-06-11 06:57:22 UTC
Permalink
Post by Dan Reinholz
My question this time is what is the preferred method
to plot a piecewise function?
As Alan said you can plot the pieces separately. Another possibility is to
define your function as in python and then plot this function as below.


# ---------------------------------------------------------------
def f(x):
if x<0: return 1
else: return 1+x

g = graph.graphxy(width=8, x=graph.axis.linear(min=-2, max=2),
y=graph.axis.linear(min=-0, max=3))

g.plot(graph.data.function("y(x)=f(x)", context=locals()))
# ---------------------------------------------------------------


Ciao,
Stefan
Dan Reinholz
2008-06-11 16:26:38 UTC
Permalink
Stefan: This is exactly what I was looking for,
thanks.

Alan: Initially I didn't understand what you were
saying, but now it makes sense. Thanks.

Everyone's help is much appreciated.

Loading...