prevent line breaks in legends

Hi, I'm looping over a list like this

    > ['name',[data_array],'name2',[data_array2],...] like
    > this:

    > for x,y in zip(toplot_list,linestyles): if type(x) ==
    > type(array()): pylab.plot(wavelengths,x,'k%s'% y)
    > pylab.legend(toplot_list[toplot_list.index(x)-1])

    > Now there is a problem with the legend, that I don't
    > see all names, but only the last. I seems to me like
    > the names get superposed, though I cannot see
    > this. Anyway, if I have a name like 'hello' the entry
    > in the legend looks like: h e l l o Well, this is not
    > quite the behaviour I wanted. Does anybody know how to
    > increase the size of the legend automatically so that
    > all names can be written out? Or is it possible to
    > prevent line break somehow?

Call the legend outside the list with a list of lines and labels you
want to pass to it. The strange 'hello' artifact you are seeing is
because 'hello' is a string and the legend code is iterating over it.

Something like

lines =
labels =
for val in something:
   lines.extend(plot(val))
   labels.append(somelabel)
legend(lines, labels)

Or using autolegend
for val in something:
   plot(val, label=somelabel)
legend()

JDH

John,

Since this appears to be a recurrent problem, would it not be better to
test for a string and then not do the iteration. In other words, the
iteration should be done only for lists and tuples.

– Paul

···

On 9/30/05, John Hunter <jdhunter@…4…> wrote:

> Hi, I'm looping over a list like this
> ['name',[data_array],'name2',[data_array2],...] like

> this:

> for x,y in zip(toplot_list,linestyles): if type(x) ==
> type(array([])): pylab.plot(wavelengths,x,'k%s'% y)
> pylab.legend(toplot_list[toplot_list.index(x)-1])


> Now there is a problem with the legend, that I don't
> see all names, but only the last. I seems to me like
> the names get superposed, though I cannot see

> this. Anyway, if I have a name like 'hello' the entry
> in the legend looks like: h e l l o Well, this is not
> quite the behaviour I wanted.  Does anybody know how to

> increase the size of the legend automatically so that
> all names can be written out? Or is it possible to
> prevent line break somehow?

Call the legend outside the list with a list of lines and labels you

want to pass to it. The strange ‘hello’ artifact you are seeing is
because ‘hello’ is a string and the legend code is iterating over it.

Something like

lines =
labels =
for val in something:

lines.extend(plot(val))
labels.append(somelabel)
legend(lines, labels)

Or using autolegend
for val in something:
plot(val, label=somelabel)
legend()

JDH


This SF.Net email is sponsored by:
Power Architecture Resource Center: Free content, downloads, discussions,
and more. http://solutions.newsforge.com/ibmarch.tmpl


Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Thanks John, works great.

Since this appears to be a recurrent problem, would it not be better to test for a string and then not do the iteration. In other words, the iteration should be done only for lists and tuples.

Well Paul, I guess this is my fault, I should have known better by know. But I actually think you are right: Since its not really self explaining and people are making the same mistake over and over again (me even twice, arrgh!), it's worth to consider your idea.

Cheers,
Christian