Reputation: 93
I tried the code below but I only received a list of all the ttf font files.
import matplotlib.font_manager as fm
for font in fm.findSystemFonts():
print font
I was thinking maybe I need to use the findFont() function from matplotlib but I'm not sure how to use it. My goal here is to be able to ask python what the default font it uses for matplotlib is and ideally if it is not Times New Roman I would like to change the default to Times New Roman. Also, if times new roman does not exist for the matplotlib library then how can I change the default to the next best serif font.
Upvotes: 2
Views: 7744
Reputation: 4219
You can modify the fonts dynamically using rcParams
. Check the following code:
import matplotlib as mpl
mpl.rcParams['font.family'] = ['serif'] # default is sans-serif
mpl.rcParams['font.serif'] = [
'Times New Roman',
'Times',
'Bitstream Vera Serif',
'DejaVu Serif',
'New Century Schoolbook',
'Century Schoolbook L',
'Utopia',
'ITC Bookman',
'Bookman',
'Nimbus Roman No9 L',
'Palatino',
'Charter',
'serif']
This way, if Times New Roman are available in your system matplotlib will use it during the session, if not, it will try to use the next font type defined. Have a look here for more info.
If you want to reset the parameters to the defaults you could use:
mpl.rcdefaults()
Other option would be to use a stylesheet with the settings you want and you could apply that style to all the plots or just to some of them using a context manager.
Upvotes: 3