Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change fonts in matplotlib (python)?

It sounds as an easy problem but I do not find any effective solution to change the font (not the font size) in a plot made with matplotlib in python.

I found a couple of tutorials to change the default font of matplotlib by modifying some files in the folders where matplotlib stores its default font - see this blog post - but I am looking for a less radical solution since I would like to use more than one font in my plot (text, label, axis label, etc).

like image 355
SirC Avatar asked Jan 23 '14 23:01

SirC


People also ask

What font does Matplotlib use?

The default font has changed from "Bitstream Vera Sans" to "DejaVu Sans".

How do I change font color in Matplotlib?

Set the figure size and adjust the padding between and around the subplots. Using rcParams['text. color'], we can get the default text color.


2 Answers

Say you want Comic Sans for the title and Helvetica for the x label.

csfont = {'fontname':'Comic Sans MS'} hfont = {'fontname':'Helvetica'}  plt.title('title',**csfont) plt.xlabel('xlabel', **hfont) plt.show() 
like image 108
aidnani8 Avatar answered Sep 20 '22 07:09

aidnani8


You can also use rcParams to change the font family globally.

 import matplotlib.pyplot as plt  plt.rcParams["font.family"] = "cursive"  # This will change to your computer's default cursive font 

The list of matplotlib's font family arguments is here.

like image 21
morepenguins Avatar answered Sep 22 '22 07:09

morepenguins