# 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...

edit retag close merge delete

Sort by » oldest newest most voted

While 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()


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.

more

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

            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.

more

1

Ok 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?