# plot operation error

I'm trying to plot the following.

def myfn2(x): if x<0: return 1 else: return -1 plot(myfn2(x),x,-3,3,figsize=3,color="red")

The graph is only displayed as -1. Why?

plot operation error

I'm trying to plot the following.

def myfn2(x): if x<0: return 1 else: return -1 plot(myfn2(x),x,-3,3,figsize=3,color="red")

The graph is only displayed as -1. Why?

2

Or simply:

```
plot(myfn,(x,-1,1))
```

HTH,

1

This is because it first evaluates myfn2(x) on the symbolic variable `x`

,
and the test `x < 0`

then returns `False`

, so what gets plotted is only `-1`

.

The workaround is this:

```
sage: def myfn2(x):
....: if x < 0:
....: return 1
....: else:
....: return -1
....:
sage: plot(lambda x: myfn2(x), (-3, 3), figsize=3, color='red')
```

Asked: **
2017-10-12 19:38:19 -0600
**

Seen: **89 times**

Last updated: **Oct 15 '17**

Why does graph plotting crop so aggressively, and what is a work-around?

Save plot in SVG with plain text strings

Plotting in sage 4.8 defaults to firefox, WHY?

ERROR: An unexpected error occurred while tokenizing input

convert a symbolic var into a numeric var

Is there a way to draw a graph using a specific embedding?

How do I plot a real function whose computation involves complex intermediate results?

Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.

Welcome to Ask Sage! Thank you for your question.