Ask Your Question
1

How to enforce limits to the x-axis in plots?

asked 2016-04-07 13:07:12 +0100

Philosophiæ Naturalis gravatar image

I want to plot a function, say $ f(x) = \frac{1}{x} $, in a certain interval, $ x \in [-2, 2] $, but produce the resulting plot in a broader interval, $ x \in [-3, 3] $, and store it as a graphical file. This case occurs, e.g., when a function is well-defined only in a certain region, but the plot should illustrate a broader region.

Currently, I pass the new $x$-axis range via show as follows:

p = plot(1/x, (x, -2, 2), ymax = 10, ymin = -10, exclude = [0])
p.show(xmin = -3, xmax = 3)
p.save("plot.svg")

Is it possible to change the $x$-axis range in a more direct way (without using show)? For instance,

p = plot(1/x, (x, -2, 2), ymax = 10, ymin = -10, exclude = [0])
p.xmin = -3 
p.xmax = 3
p.save("plot.svg")
edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
2

answered 2016-04-07 15:03:02 +0100

tmonteil gravatar image

GIven a Python/Sage object p, you can get the list of available methods by doing p.<TAB_BUTTON>, so you can see that there is a set_axes_range method. Then, you can see its documentation by doing p.set_axes_range?. In you case, you can do:

sage: p.set_axes_range(xmin=-3, xmax=3)
edit flag offensive delete link more
1

answered 2016-04-07 21:05:40 +0100

Eugene gravatar image

May I ask why you are not satisfied with using xmin and xmax keywords? Note that you can also pass them to save:

p.save("plot2.svg", xmin=-3, xmax=3)
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-04-07 13:07:12 +0100

Seen: 2,341 times

Last updated: Apr 07 '16