# legend overlaps plot

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 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.

Sort by » oldest newest most voted 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: 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)

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!

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.

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?

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