Ask Your Question

Revision history [back]

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.