3

I have a couple of lines and I want to show a legend. The problem is, I can't use different styles (--, :, -.) because there are too few of them, and I can't use markers (+, *, etc.) because I need them to show some points on the lines.

So the best idea I've come up with is to use numbers. But I can't figure how I can create legends with numbers. I can even draw numbers near lines myself (to place them in the best position), but how can I then draw a legend with the numbers?

I.e. instead of:

--  H
-.- Li

I'd like something like:

1  H
2  Li
4
  • Well, with the four line styles, seven reasonable colors, and linewidths, you can create many combinations... at least more than the maximum number of plots that should go on any single figure. :-) Commented Nov 1, 2010 at 3:32
  • Oh, forgot to say, this graph will be printed in black-and-white paper, so I can't use colors :) Different line widths also not a good solution IMHO... Commented Nov 1, 2010 at 3:37
  • At that point, why not just label the curves directly? Putting 1, 2, etc, and then having a legend would seem to make things confusing... Why not just put a H or Li where you would put the 1 or 2? If you really need, though, it's not too difficult to do what you want (it's easiest to just roll your own legend), but it seems like a bad idea, in my opinion... Commented Nov 1, 2010 at 3:45
  • Yes, this is not bad temporary solution, I'll try to do that, thanks! But this could work for current graph with short labels. Most likely I'll have graphs with longer labels and if I put them near lines, it'll look messy. Could you please share how to add numbers into legend box, please? :) Commented Nov 1, 2010 at 3:56

1 Answer 1

3

Perhaps a little Latex thrown into the mix?

#In which we make a legend; not with lines, but numbers!

import pylab as pl
pl.rc('text', usetex=True)

pl.figure(1)
pl.clf()
ax = pl.subplot(111)
pl.plot(range(0,10), 'k', label = r'\makebox[25]{1\hfill}Bla')
pl.plot(range(1,11), 'k', label = r'\makebox[25]{12\hfill}Bla12')
lgd = pl.legend(handlelength = -0.4)
for k in lgd.get_lines():
k.set_linewidth(0)
pl.draw()
pl.show()

The numbers/labels are aligned by using \makebox with specific width and \hfill to take up the space not used by your labels. Numbers are not automatic, but if you use a loop to draw your lines then you could add a counter to keep track of the numbers.

Don't know if this is part of your requirement, but the lines are removed by setting their linewidth to 0 and making the space reserved in the legend negative. Couldn't find a neater way of doing this as I believe a legend is always meant to show a line (e.g. you can't set numpoints to 0).

You could of course also just add some text in the right spot in your plot and not use a legend at all.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! It works, though I have some problems. Sometimes my MiKTeX falls, I need to comment pl.rc('text', usetex=True), then run file, then uncomment, then run again - then it'll work. I didn't investigate further :) Also Russian letters don't work. But in general it's working solution, thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.