Ask Your Question
1

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

asked 2013-05-31 16:59:16 +0200

AndreWin gravatar image

updated 2013-05-31 17:28:27 +0200

tmonteil gravatar image

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

2 Answers

Sort by ยป oldest newest most voted
2

answered 2013-06-01 02:54:35 +0200

AndreWin gravatar image

updated 2013-06-01 03:02:14 +0200

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)
    fig.add_subplot(ax)
    L = sage_plot.matplotlib().gca().lines
    for t in L:
        data = t.get_data()
        ax.add_line(mpl.lines.Line2D(data[0], data[1]))
    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!))

edit flag offensive delete link more

Comments

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

tmonteil gravatar imagetmonteil ( 2013-06-01 07:24:40 +0200 )edit
1

answered 2013-05-31 17:30:00 +0200

tmonteil gravatar image

updated 2013-05-31 19:59:03 +0200

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)
fig.add_subplot(ax)
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.

edit flag offensive delete link more

Comments

Thanks a lot!)

AndreWin gravatar imageAndreWin ( 2013-06-01 01:57:45 +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

Stats

Asked: 2013-05-31 16:59:16 +0200

Seen: 4,236 times

Last updated: Jun 01 '13