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

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 close merge delete

Sort by » oldest newest most voted

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)

more

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)

more