19

What is the best way to specify the font weight for a matplotlib legend? I can use:

matplotlib.rcParams.update({'legend.fontsize':12})

to set the font size but when I use

matplotlib.rcParams.update({'legend.fontweight':'bold'}

It complains that "'legend.fontweight' is not a valid rc parameter"

1 Answer 1

16

You can pass parameters into plt.legend using the prop argument. This dictionary allows you to select text properties for the legend.

import matplotlib
import matplotlib.pyplot as plt

legend_properties = {'weight':'bold'}

plt.plot([1,2,3], [4,5,6], label='Test')
plt.legend(prop=legend_properties)

plt.show()

example plot

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for this answer! You can include the properties on the same line as the call to legend, like: plt.legend(prop=dict(weight='bold')
Is it possible to set this value in the mpstyle file?
..also, if you like to set bold text for a specific legend record, you may enumerate legend.get_texts() and call appropriate set_weight() for each record.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.