ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 07 Apr 2016 14:05:40 -0500How to enforce limits to the x-axis in plots?https://ask.sagemath.org/question/33032/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")Thu, 07 Apr 2016 06:07:12 -0500https://ask.sagemath.org/question/33032/how-to-enforce-limits-to-the-x-axis-in-plots/Answer by tmonteil for <p>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.</p>
<p>Currently, I pass the new $x$-axis range via <code>show</code> as follows:</p>
<pre><code>p = plot(1/x, (x, -2, 2), ymax = 10, ymin = -10, exclude = [0])
p.show(xmin = -3, xmax = 3)
p.save("plot.svg")
</code></pre>
<p>Is it possible to change the $x$-axis range in a more direct way (without using <code>show</code>)? For instance, </p>
<pre><code>p = plot(1/x, (x, -2, 2), ymax = 10, ymin = -10, exclude = [0])
p.xmin = -3
p.xmax = 3
p.save("plot.svg")
</code></pre>
https://ask.sagemath.org/question/33032/how-to-enforce-limits-to-the-x-axis-in-plots/?answer=33034#post-id-33034GIven 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)
Thu, 07 Apr 2016 08:03:02 -0500https://ask.sagemath.org/question/33032/how-to-enforce-limits-to-the-x-axis-in-plots/?answer=33034#post-id-33034Answer by Eugene for <p>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.</p>
<p>Currently, I pass the new $x$-axis range via <code>show</code> as follows:</p>
<pre><code>p = plot(1/x, (x, -2, 2), ymax = 10, ymin = -10, exclude = [0])
p.show(xmin = -3, xmax = 3)
p.save("plot.svg")
</code></pre>
<p>Is it possible to change the $x$-axis range in a more direct way (without using <code>show</code>)? For instance, </p>
<pre><code>p = plot(1/x, (x, -2, 2), ymax = 10, ymin = -10, exclude = [0])
p.xmin = -3
p.xmax = 3
p.save("plot.svg")
</code></pre>
https://ask.sagemath.org/question/33032/how-to-enforce-limits-to-the-x-axis-in-plots/?answer=33039#post-id-33039May 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)
Thu, 07 Apr 2016 14:05:40 -0500https://ask.sagemath.org/question/33032/how-to-enforce-limits-to-the-x-axis-in-plots/?answer=33039#post-id-33039