ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 13 Sep 2016 03:27:37 +0200How to change position of the axes values of a Sage plot.https://ask.sagemath.org/question/33387/how-to-change-position-of-the-axes-values-of-a-sage-plot/ I am plotting 2D functions on Sage, and I would like to move the values displayed along the $y$ axis (the ticks) from the left of the axis to the rigth, because they bother me there. I had a look and I think this can be done in Python with matplotlib (using `set_tick_params()` or `set_ticks_position()`) which is included in Sage, but I am not sure of how to combine a `region_plot()` from Sage with this axis modification.
¿Anyway to do what I am saying? I can't include an example of what I am saying because I don't have enough Karma to add a figure or link...Thu, 12 May 2016 18:01:18 +0200https://ask.sagemath.org/question/33387/how-to-change-position-of-the-axes-values-of-a-sage-plot/Comment by kcrisman for <p>I am plotting 2D functions on Sage, and I would like to move the values displayed along the $y$ axis (the ticks) from the left of the axis to the rigth, because they bother me there. I had a look and I think this can be done in Python with matplotlib (using <code>set_tick_params()</code> or <code>set_ticks_position()</code>) which is included in Sage, but I am not sure of how to combine a <code>region_plot()</code> from Sage with this axis modification.</p>
<p>¿Anyway to do what I am saying? I can't include an example of what I am saying because I don't have enough Karma to add a figure or link...</p>
https://ask.sagemath.org/question/33387/how-to-change-position-of-the-axes-values-of-a-sage-plot/?comment=33400#post-id-33400See also http://math.stackexchange.com/questions/1781126/how-to-change-position-of-the-axes-values-of-a-sage-plotFri, 13 May 2016 15:13:48 +0200https://ask.sagemath.org/question/33387/how-to-change-position-of-the-axes-values-of-a-sage-plot/?comment=33400#post-id-33400Answer by paulmasson for <p>I am plotting 2D functions on Sage, and I would like to move the values displayed along the $y$ axis (the ticks) from the left of the axis to the rigth, because they bother me there. I had a look and I think this can be done in Python with matplotlib (using <code>set_tick_params()</code> or <code>set_ticks_position()</code>) which is included in Sage, but I am not sure of how to combine a <code>region_plot()</code> from Sage with this axis modification.</p>
<p>¿Anyway to do what I am saying? I can't include an example of what I am saying because I don't have enough Karma to add a figure or link...</p>
https://ask.sagemath.org/question/33387/how-to-change-position-of-the-axes-values-of-a-sage-plot/?answer=34807#post-id-34807While I haven't yet figured out how to get a modified subplot reintegrated into a SageMath plot, here's a way to modify the y-axis to move the labels to the right and view the change. First set up variables like so:
p = plot(sin(x), (x, -2*pi, 2*pi))
fig = p.matplotlib()
subplot = fig.get_axes()[0]
In matplotlib, `axes` means subplots. Notice I didn't create a new subplot, just retrieved the existing one. Now move the labels with these methods:
subplot.yaxis.tick_right()
subplot.spines['right'].set_position('center')
The first method flips the labels to the right-hand side but also moves them to the right side of the plot. The second method move them back to the center of the plot.
If you `show(p)` at this point you'll just get the original plot: the methods just applied unfortunately don't automatically modify the original plot. To see the change, you need to save the modified figure first:
from matplotlib.backends.backend_agg import FigureCanvasAgg
fig.set_canvas(FigureCanvasAgg(fig))
fig.savefig('fig')
Viewing the saved figured depends on where you're running Sage. For desktop versions the figure goes into the directory where Sage starts, not the hidden temporary directory, so you should be able to see it with this,
from PIL import Image
image = Image.open('fig.png')
image.show()
or you can just use a file explorer to find it.
Tue, 13 Sep 2016 03:27:37 +0200https://ask.sagemath.org/question/33387/how-to-change-position-of-the-axes-values-of-a-sage-plot/?answer=34807#post-id-34807Answer by kcrisman for <p>I am plotting 2D functions on Sage, and I would like to move the values displayed along the $y$ axis (the ticks) from the left of the axis to the rigth, because they bother me there. I had a look and I think this can be done in Python with matplotlib (using <code>set_tick_params()</code> or <code>set_ticks_position()</code>) which is included in Sage, but I am not sure of how to combine a <code>region_plot()</code> from Sage with this axis modification.</p>
<p>¿Anyway to do what I am saying? I can't include an example of what I am saying because I don't have enough Karma to add a figure or link...</p>
https://ask.sagemath.org/question/33387/how-to-change-position-of-the-axes-values-of-a-sage-plot/?answer=33399#post-id-33399This is definitely possible using the matplotlib object underlying the plot, or even by hacking the underlying Sage code that tells it to go there. See plot/graphics.py, esp. lines like
subplot.spines['right'].set_visible(False)
subplot.spines['left'].set_position(('outward',10))
subplot.yaxis.set_ticks_position('left')
subplot.yaxis.set_label_position('left')
which should give you an idea of what matplotlib stuff to fiddle with.Fri, 13 May 2016 15:07:47 +0200https://ask.sagemath.org/question/33387/how-to-change-position-of-the-axes-values-of-a-sage-plot/?answer=33399#post-id-33399Comment by iipr for <p>This is definitely possible using the matplotlib object underlying the plot, or even by hacking the underlying Sage code that tells it to go there. See plot/graphics.py, esp. lines like</p>
<pre><code> subplot.spines['right'].set_visible(False)
subplot.spines['left'].set_position(('outward',10))
subplot.yaxis.set_ticks_position('left')
subplot.yaxis.set_label_position('left')
</code></pre>
<p>which should give you an idea of what matplotlib stuff to fiddle with.</p>
https://ask.sagemath.org/question/33387/how-to-change-position-of-the-axes-values-of-a-sage-plot/?comment=33402#post-id-33402Ok I think I can't figure this out. I read `plot/graphics.py` and I tried heaps of things like:
`p = plot(sin(x), (x, -2*pi, 2*pi)); fig = p.matplotlib(); subplot = fig.add_subplot(111); subplot.yaxis.set_ticks_position('right'); subplot.yaxis.set_label_position('right')`
But I don't understand how to plot this new modified matplotlib figure, could you please provide me with some more guidance?Fri, 13 May 2016 18:33:30 +0200https://ask.sagemath.org/question/33387/how-to-change-position-of-the-axes-values-of-a-sage-plot/?comment=33402#post-id-33402