Ask Your Question

legend overlaps plot

asked 2010-11-16 14:56:58 -0500

ryan gravatar image

updated 2010-11-17 10:01:26 -0500

The legend_label overlaps my plot (ie the plotted lines). How do I prevent this? It would be nice to move the legend beside or under the plot (outside of the plot axis area).

for example: plot(cos(x), (x,-2pi, 2pi), legend_label='cos(x)')+plot(cos(3x), (x,-2pi, 2*pi), color='green', legend_label='cos(3x)')

Is it possible to get the legend to appear in a subaxes beside the plot?

edit retag flag offensive close merge delete


If you can give us the specific code you used, that would help. Probably this indicates some bug in our our padding for the legend, or some customization we haven't wrapped yet, but it's hard to be sure.

kcrisman gravatar imagekcrisman ( 2010-11-17 05:39:21 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2010-11-17 11:24:50 -0500

updated 2010-11-17 11:25:29 -0500

You can do something like this:

sage: P = plot(cos(x), (x,-2*pi, 2*pi), legend_label='cos(x)')+plot(cos(3*x), (x,-2*pi, 2*pi), color='green', legend_label='cos(3x)')
sage: P.ymin(-1.5)

Then evaluating P will yield this picture: image description

Is this the sort of thing you want? Note that you could also include ymin=-1.5 in the command defining the plot:

sage: plot(cos(x), (x,-2*pi, 2*pi), legend_label='cos(x)')+plot(cos(3*x), (x,-2*pi, 2*pi), color='green', legend_label='cos(3x)', ymin=-1.5)
edit flag offensive delete link more


Nice! Although it might still be worth filing a ticket... if mpl/Sage can detect when the plot touches the stuff. Or to add this great example to the documentation!

kcrisman gravatar imagekcrisman ( 2010-11-17 15:54:41 -0500 )edit

This will work for the current project that I'm working on. However, I would like to see if sage could automatically handle situations like these.

ryan gravatar imageryan ( 2010-11-22 01:00:00 -0500 )edit

Which version does that work in? I tried copy&pasting your code in notebook and in sage prompt, and it doesn't work for me in 4.5.3. I get a RuntimeError in line(), illegal option `legend_label`. Are you using some other plot lib? Can you complete your example with the necessary import ... in that case?

mandrake gravatar imagemandrake ( 2010-11-23 02:54:39 -0500 )edit

I think this would have to be 4.6; legend labels were pretty recently introduced (from matplotlib).

kcrisman gravatar imagekcrisman ( 2010-11-23 03:10:30 -0500 )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


Asked: 2010-11-16 14:56:58 -0500

Seen: 1,209 times

Last updated: Nov 17 '10