python - How to change legend fontname in matplotlib

ID : 274305

viewed : 40

Tags : pythonmatplotliblegendlegend-propertiespython

Top 4 Answer for python - How to change legend fontname in matplotlib

vote vote


This wasn't showing up in google results so I'm going to post it as an answer. The rc parameters for font can be used to set a single default font.

Solution for anyone else with the same issue:

import matplotlib as mpl mpl.rc('font',family='Times New Roman') 
vote vote


The .rc solution given changes the default font for all drawing.

Here is a solution for doing this when you don't want to change all the fonts, but just the font properties of the legend of this particular graph (a legend belonging to a particular axis object):

L = ax.legend() plt.setp(L.texts, family='Consolas') 

This allows you to choose a different font for the legend and the axes. I found this helpful when I needed a monospace font for my legend, but not for the axes -- allowing me to create a neat legend like this:

enter image description here

Note how the title is a different font than the legend - this gives me an alignment of numbers that would otherwise be hard to achieve.

vote vote


I think this is the better way.

import matplotlib.font_manager as fm  ## your font directory  font_path = '/Users/frhyme/Library/Fonts/BMDOHYEON_otf.otf'  ## font_name  font_name = fm.FontProperties(fname=font_path).get_name()  plt.legend(prop={'family':font_name, 'size':20}) 
vote vote


Top 3 video Explaining python - How to change legend fontname in matplotlib