matplotlib legend shows "Container object of 10 artists" instead of label

Roy

The code below produces the plot and legend correctly, however the legend does not show the specified label text, but "Container object of 10 artists" instead.

ax = plt.subplot(212)

plt.tight_layout()

l1= plt.bar(X+0.25, Y1, 0.45, align='center', color='r', label='A', edgecolor='black', hatch="/")
l2= plt.bar(X,      Y2, 0.45, align='center', color='b', label='N',hatch='o', fill=False)

ax.autoscale(tight=True)

plt.xticks(X, X_values)

ymax1 = max(Y1) + 1
ymax2 = max(Y2) + 1

ymax = max(ymax1,ymax2)+1
plt.ylim(0, ymax)

plt.grid(True)

plt.legend([l1,l2], loc='upper right', prop={'size':20})

The output is shown below:

Plot showing the missing labels

How can I correctly display the labels for each bar (as specified in the plt.bar() function) on the legend?

mfitzp

The problem stems from mixing two approaches to using plt.legend(). You have two options:

  1. Manually specify the labels for the legend
  2. Use ax.get_legend_handles_labels() to fill them in with the label parameters you passed to plt.bar()

To manually specify the labels, pass them as the second argument to your call to plt.legend() as follows:

plt.legend([l1,l2], ["A", "N"], loc='upper right', prop={'size':20})

If instead you want to automatically populate the legend you can use the following to find legend-able objects in the plot and their labels:

handles, labels = ax.get_legend_handles_labels()
ax.legend(handles, labels, loc='upper right', prop={'size':20})

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Label is Missing from matplotlib legend

From Dev

Rotating legend or adding patch to axis label in matplotlib

From Dev

Rotating legend or adding patch to axis label in matplotlib

From Dev

Legend only shows one label when plotting with pandas

From Dev

matplotlib graph shows only points instead of line

From Dev

python matplotlib legend shows first entry of a list only

From Dev

How to make two markers share the same label in the legend using matplotlib?

From Dev

Create legend for scatter plot using the label of the samples in matplotlib

From Dev

matplotlib, including more than one defined variable on legend label

From Java

How to make lines instead of boxes/rectangles in a matplotlib legend of a histogram

From Dev

Input field shows model value instead of label after select

From Dev

Legend in matplotlib

From Dev

Custom Legend Label

From Dev

Matplotlib scatter plot with legend

From Dev

Matplotlib legend for an arrow

From Dev

Matplotlib legend fontsize

From Dev

tabular legend layout for matplotlib

From Dev

Matplotlib Stackplot legend Error

From Java

Matplotlib scatter plot legend

From Dev

Duplicate items in legend in matplotlib?

From Dev

Shade legend line in matplotlib

From Dev

matplotlib legend background color

From Dev

legend alignment in matplotlib

From Dev

Multiple titles in legend in matplotlib

From Dev

Align legend rows in matplotlib

From Dev

Customizing legend in matplotlib

From Dev

Get Matplotlib legend location?

From Dev

Matplotlib editing legend labels

From Dev

Getting the width of a legend in matplotlib