Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to add an empty entry to a Legend in Matplotlib?

I wanted to add a blank space to a legend in a matplotib plot in python. I have an odd number of entries in the legend, and it currently looks like:

 _________________________________________________________________
|  handle1  -  label1   handle3  -  label3   handle5  -  label5  |
|  handle2  -  label2   handle4  -  label4                       |

However, the data is logically grouped in pairs with a control, so it would be better if it looked like this:

 _________________________________________________________________
|  handle1  -  label1   handle2  -  label2   handle4  -  label4  |
|                       handle3  -  label3   handle5  -  label5  |

The legend is being generated during automatically running a for loop over the data set:

for [folder1,folder2, label] in folder_list:
    parse_folder([folder1,folder2])
    color = next(colorgen)
    marker = next(markergen)
    ax1.errorbar(percent[0],percent[1], yerr=per_std, c=color, fmt=marker, label=label)
    if label == 'Flat Plate':
        print 'tripped'
        ax1.plot(np.NaN, np.NaN, '-', color='none', label=' ')

and then at the end calling

leg = ax1.legend(loc='lower left',fancybox=True,prop={'size':fontsize-2},ncol=4,bbox_to_anchor=(-0.1, -0.315))

Is there a way to insert this blank spot into the legend?

like image 996
Elliot Avatar asked Jan 21 '15 23:01

Elliot


People also ask

How do you skip a legend entry in Python?

@sid100158- To exclude one or more elements from the legend, pass no label or label='nolegend'. for your question you can use this command to exclude one or more elements from the legend. Hope this helps!


2 Answers

Farenorth's solution (given in the comments to the Op) give the cleanest answer.

Add an additional command in the position that you need the blank spot, using np.NaN for data and setting the color to none.

ax1.plot(np.NaN, np.NaN, '-', color='none', label='')

Note that this must be of the same type, as the method used to automatically generate the Legend gets different plot types sequentially, ie all plot, then all errorbar, etc., so the blank entry needs to be the same type to stay in order of the function calls.

like image 198
Elliot Avatar answered Oct 16 '22 01:10

Elliot


You can define a fake white line

l = Line2D([0],[0],color="w")

Then plot your data and save the line/markers in variables

f = figure()
ax = f.add_subplot(111)

l1, = ax.plot(1*arange(10))
l2, = ax.plot(2*arange(10))
l3, = ax.plot(3*arange(10))
l4, = ax.plot(4*arange(10))
l5, = ax.plot(5*arange(10))

Finally, you call legend as follows

ax.legend((l1,l,l2,l3,l4,l5),("label1","","label2","label3","label4","label5"),
           loc='upper center',fancybox=True,ncol=3)

where for each line/marker is associated with a different label. At the place where you want to have a gap in your legend insert the fake white line l and associate a blank string to it.

Hope it helps.enter image description here

like image 29
mrcl Avatar answered Oct 16 '22 03:10

mrcl