Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib: figlegend only printing first letter

I try to print a figlegend with only one line, but I only get the first letter. I have the following script for making the plot:

from pylab import *
k = plot((0, 1),(1, 1))
figlegend((k),('Limit'),loc='lower center')
savefig('test.pdf')

The output is: output

What am I doing wrong? (Or is it a bug?)

like image 503
Torstein I. Bø Avatar asked May 11 '12 19:05

Torstein I. Bø


People also ask

Is PLT show () necessary?

draw() . Using plt. show() in Matplotlib mode is not required.

What is PLT legend () in Python?

Plot legends give meaning to a visualization, assigning meaning to the various plot elements. We previously saw how to create a simple legend; here we'll take a look at customizing the placement and aesthetics of the legend in Matplotlib.

How do I change the order of my legend in Matplotlib?

Change order of items in the legend The above order of elements in the legend region can be changed by the gca method that uses another sub-method called get_legend_handles_labels method. These handles and labels lists are passed as parameters to legend method with order of indexes.


2 Answers

I haven't figured out whether it is a bug or intentional (for some reason) in matplotlib, but in order to get a full legend label you need to leave a trailing comma on your list of labels:

figlegend((k),('Limit',),loc='lower center')

change that line and your code:

from pylab import *
k = plot((0, 1),(1, 1))
figlegend((k),('Limit',),loc='lower center')
savefig('test.pdf')

produces the figure:

full legend label

Alternatively, one can use [] to achieve the same result:

figlegend((k),(['Limit']),loc='lower center')
like image 82
BFTM Avatar answered Oct 04 '22 04:10

BFTM


The answer to your problem is the following.

For the names of the legend, you have to surround it in square brackets, like this:

figlegend((k),[('Limit')],loc='lower center')

as you can see the legend name 'limit' is surrounded in square brackets, and this will then display the full name.

Here would be the full code:
from pylab import *
k = plot((0, 1),(1, 1))
figlegend((k),[('Limit')],loc='lower center')
savefig('test.pdf')
like image 40
King Avatar answered Oct 04 '22 04:10

King