Skip to content

Instantly share code, notes, and snippets.

@binaryfunt
Created September 25, 2015 10:44
Show Gist options
  • Save binaryfunt/a8bf0238eaaf574ccb47 to your computer and use it in GitHub Desktop.
Save binaryfunt/a8bf0238eaaf574ccb47 to your computer and use it in GitHub Desktop.
Change fonts on Matplotlib plots in Python
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager
# Set the font properties (can use more variables for more fonts)
font_path = 'C:\Windows\Fonts\AGaramondPro-Regular.otf'
font_prop = font_manager.FontProperties(fname=font_path, size=14)
ax = plt.subplot() # Defines ax variable by creating an empty plot
# Simulate some data to be plotted
x = np.linspace(0, 10)
y = x + np.random.normal(x)
plt.plot(x, y, 'b+', label='Data points')
for label in (ax.get_xticklabels() + ax.get_yticklabels()):
label.set_fontproperties(font_prop)
label.set_fontsize(13) # Size here overrides font_prop
plt.title("Check this out", fontproperties=font_prop,
size=16, verticalalignment='bottom') # Size here overrides font_prop
plt.xlabel("X axis", fontproperties=font_prop)
plt.ylabel("Y axis", fontproperties=font_prop)
plt.legend(loc='lower right', prop=font_prop) # NB different 'prop' argument for legend
plt.text(0, 0, "Misc text", fontproperties=font_prop)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment