Ask Your Question
4

legend overlaps plot

asked 2010-11-16 21:56:58 +0100

ryan gravatar image

updated 2010-11-17 17:01:26 +0100

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

Comments

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 12:39:21 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
5

answered 2010-11-17 18:24:50 +0100

updated 2010-11-17 18:25:29 +0100

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

Comments

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 22:54:41 +0100 )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 08:00:00 +0100 )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 09:54:39 +0100 )edit

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

kcrisman gravatar imagekcrisman ( 2010-11-23 10:10:30 +0100 )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: 2010-11-16 21:56:58 +0100

Seen: 2,690 times

Last updated: Nov 17 '10