How to add a custom font in plotnine
Plotnine is a package that brings the grammar of graphics, similar to {ggplot2} in R, to Python.
Like {ggplot2}, plotnine
supports the use of custom fonts. To add a custom font:
1import matplotlib.font_manager as fm
import os
2= os.getcwd()
project_dir = os.path.join(project_dir, "Raleway-VariableFont_wght.ttf")
font_path = fm.FontProperties(fname=font_path)
prop
= (
p ="date", y="rate", color="type"))
ggplot(rates_long, aes(x+ geom_line()
+ theme(
3=element_text(hjust=0, fontproperties=prop),
title=element_text(fontproperties=prop),
text=element_rect(fill="white")
plot_background
) )
- 1
-
Install
matplotlib
’s font_manager. - 2
-
Install the custom font on your system, and use the
os
package to point to the directory with your custom font. - 3
-
Set the font in your
plotnine
plot using thetheme()
function.
I posted a plotnine
plot on Reddit using the Raleway font (code here):
15- and 30-Year Fixed Rate Mortgage Average in the United States [OC]
byu/ionychal indataisbeautiful