Customise legend labels

I’m trying to customise individual legend labels. In the example below, the legend contains two items. I’d like to make the text bold for only the second legend label.

Here’s a general outline of the code:

leg = plt.legend()
for text in leg.get_texts():
    text.set_fontweight...

Here’s a runnable example:

X=range(10)
Y=range(100,110)
Z=range(105,115)
plt.plot(X,Y,label='normal')
plt.plot(X,Z,label='bold')
fontweights=['normal','bold']
leg=plt.legend()
for fw,text in zip(fontweights,leg.get_texts()):
    text.set_fontweight(fw)
plt.show()

The plot produced shows that set_fontweight() changes both labels to bold. So is this a bug with set_fontweight(), or am I doing something wrong?

Similar functions, such as text.set_color(), can be used to modify legend labels individually.

Lastly, I’m using version 3.2.2.

Thanks!

Please provide a standalone runnable example!

Thanks for the reply—I’ve just updated the post.

Sorry, that works for me?

Hi @jklymak,

In the example below, the legend contains two items. I’d like to make the text bold for only the second label. If this is the case for you, can you please tell me what matplotlib version you have?

I’ve added a bit more detail to the original post.

Thanks,
Alex

In general, if not otherwise stated, its assumed folks are talking about the latest release, which is 3.4.1. If you are not using the latest release and you have a bug, it is not likely to get fixed.

Thanks. So you see only the label for second legend entry in bold with version 3.4.1? (I’m using 3.2.2)

Yes. I guess there was a bug in 3.2