Created
September 25, 2015 10:44
-
-
Save binaryfunt/a8bf0238eaaf574ccb47 to your computer and use it in GitHub Desktop.
Change fonts on Matplotlib plots in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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