Reputation: 13
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:
import matplotlib.pyplot as plt
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()
Here's the output: enter image description here
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 matplotlib version 3.2.2.
Thanks!
Upvotes: 1
Views: 315
Reputation: 175
I have matplotlib 3.3.4 running, and get a bold and unbold legend entry - see attached image - so I think an upgrade should fix the problem.
Upvotes: 1