Aabestfontgenerator.
THE TYPE NOTEBOOK

How to Set Matplotlib Legend Font Size

Set Matplotlib legend labels and title sizes independently. Learn when FontProperties takes priority and proof the exported legend.

Pass a size to the legend call to change its entries. Give the title its own setting when the legend needs a clearer hierarchy.

Sources reviewed .

01

Set an entry size on the legend

For an Axes with labeled plots, try ax.legend(fontsize=9). Matplotlib documents numeric sizes in points. Relative values such as small are interpreted against the default. If you pass a prop object for the entry font, set the size on that object because fontsize only applies when prop is absent.

02

Give the legend title its own size

Try ax.legend(title="Series", fontsize=9, title_fontsize=11). That call names the title and gives it a different size from the labels. Matplotlib treats title_fontproperties and title_fontsize as alternatives. If you need several title font properties, supply the size through title_fontproperties.

03

Proof the longest entry in the exported plot

Render the figure at the dimensions your report will use. Compare the longest legend entry with the plot and see whether a multi-column legend or a separate placement gives labels more room. Keep tick and axis title settings independent. Use the Matplotlib axis label guide for those roles and the homepage text tool for a separate caption.

GOOD QUESTIONS

Your questions, answered.

Why does fontsize have no effect with prop set?

Matplotlib uses prop for the entry font when supplied. Include the size in that configuration.

Does the entry size also set the legend title?

Use title_fontsize or title_fontproperties to control the title explicitly.

Sources & background

Sources supporting this page’s explanations. Practical examples are our editorial suggestions.