First time here? Check out the FAQ!

Ask Your Question
4

legend overlaps plot

asked 14 years ago

ryan gravatar image

updated 14 years ago

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?

Preview: (hide)

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 ( 14 years ago )

1 Answer

Sort by » oldest newest most voted
5

answered 14 years ago

updated 14 years ago

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)
Preview: (hide)
link

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 ( 14 years ago )

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 ( 14 years ago )

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 ( 14 years ago )

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

kcrisman gravatar imagekcrisman ( 14 years ago )

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: 14 years ago

Seen: 2,734 times

Last updated: Nov 17 '10