Ask Your Question
1

How to change position of the axes values of a Sage plot.

asked 2016-05-12 18:01:18 +0200

iipr gravatar image

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

Comments

kcrisman gravatar imagekcrisman ( 2016-05-13 15:13:48 +0200 )edit

2 Answers

Sort by » oldest newest most voted
3

answered 2016-09-13 03:27:37 +0200

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()[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.

edit flag offensive delete link more
1

answered 2016-05-13 15:07:47 +0200

kcrisman gravatar image

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.

edit flag offensive delete link more

Comments

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?

iipr gravatar imageiipr ( 2016-05-13 18:33:30 +0200 )edit

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-05-12 18:01:18 +0200

Seen: 2,142 times

Last updated: Sep 13 '16