# How can I add arrows at the end of sage plot axis?

Hello everybody!

I have my plot:

import matplotlib.pyplot as plt
plt.clf()
fig = plt.figure()
plot(f(x), (0, 2*pi)).matplotlib(figure = fig)
plt.savefig('')
plt.close()


I want to add arrows at the end of this graph (as on this image). But I can't do this. Help me please.

edit retag close merge delete

Sort by » oldest newest most voted

To add an arrow somewhere, you can try:

sage: plt.arrow(1,1,1,-1,color='orange',head_width=.1,head_length=.2)


to see all options, type:

sage: plt.arrow?


To add an arrow along the axes with matplotlib, this is a bit harder since matplotlib understands the Sage plot as an image, hence you should find where to add the axes. Here is a dirty example that try to stick to your framework:

f(x) = cos(x)
from mpl_toolkits.axes_grid.axislines import SubplotZero

import matplotlib.pyplot as plt
plt.clf()
fig = plt.figure()
p = plot(f(x), (0, 2*pi))
# we store the position of the axes of the Sage plot, for a further use in the matplotlib plot.
L = p.get_axes_range()

# Here are the axes
ax = SubplotZero(fig, 111)
for direction in ["xzero", "yzero"]:
ax.axis[direction].set_axisline_style("-|>")
ax.axis[direction].set_visible(True)
for direction in ["left", "right", "bottom", "top"]:
ax.axis[direction].set_visible(False)

# here we shift the axes according to the Sage plot:
offset = 0.12 # dirty adjustment
ax.set_xlim(L['xmin']-offset,L['xmax'])
ax.set_ylim(L['ymin'],L['ymax'])

p.matplotlib(figure = fig)

fig.savefig('')
plt.close()


There should be a better solution.

Unfortunately, there seems not to be a solution from Sage's plot yet, see trac ticket 10740.

more

Wait a minute! I found the answer to my question! The graph line of sage plot converted to matplotlib is matplotlib.lines.Line2D object and this line has data - point coordinates for drawing it! So we can get these coordinates and draw our graph line in maplotlib figure! It's fine!!!!

import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid.axislines import SubplotZero

def mplot(sage_plot, equal_scale = False, edit_res = False):
"""
This function convert sage_plot, created at sage, in matplotlib graph.
"""
plt.clf()
fig = plt.figure()
ax = SubplotZero(fig, 111)
L = sage_plot.matplotlib().gca().lines
for t in L:
data = t.get_data()
ax.autoscale_view()
if equal_scale:
ax.axis('equal')
for direction in ["xzero", "yzero"]:
ax.axis[direction].set_axisline_style("-|>", size = 2)
ax.axis[direction].set_visible(True)
for direction in ["left", "right", "bottom", "top"]:
ax.axis[direction].set_visible(False)
ax.axis["yzero"].set_axis_direction("left")
ax.minorticks_on()
ax.grid()
if edit_res:
return(fig)
else:
plt.savefig('')
plt.close()


Then in sage notebook:

 sage: gr = plot(sin(x), x, (0, 2*pi))
sage: mplot(gr)


Perfect!))

more

Looks better ! Perhaps could you help adding the "arrows on the end of axes" feature into sage by proposing a patch to [trac ticket 10740](http://trac.sagemath.org/sage_trac/ticket/10740) ?