Note
Go to the end to download the full example code
Fonts demo (object-oriented style)#
Set font properties using setters.
See Fonts demo (keyword arguments) to achieve the same effect using keyword arguments.
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
fig = plt.figure()
alignment = {'horizontalalignment': 'center', 'verticalalignment': 'baseline'}
yp = [0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2]
heading_font = FontProperties(size='large')
# Show family options
fig.text(0.1, 0.9, 'family', fontproperties=heading_font, **alignment)
families = ['serif', 'sans-serif', 'cursive', 'fantasy', 'monospace']
for k, family in enumerate(families):
font = FontProperties()
font.set_family(family)
fig.text(0.1, yp[k], family, fontproperties=font, **alignment)
# Show style options
styles = ['normal', 'italic', 'oblique']
fig.text(0.3, 0.9, 'style', fontproperties=heading_font, **alignment)
for k, style in enumerate(styles):
font = FontProperties()
font.set_family('sans-serif')
font.set_style(style)
fig.text(0.3, yp[k], style, fontproperties=font, **alignment)
# Show variant options
variants = ['normal', 'small-caps']
fig.text(0.5, 0.9, 'variant', fontproperties=heading_font, **alignment)
for k, variant in enumerate(variants):
font = FontProperties()
font.set_family('serif')
font.set_variant(variant)
fig.text(0.5, yp[k], variant, fontproperties=font, **alignment)
# Show weight options
weights = ['light', 'normal', 'medium', 'semibold', 'bold', 'heavy', 'black']
fig.text(0.7, 0.9, 'weight', fontproperties=heading_font, **alignment)
for k, weight in enumerate(weights):
font = FontProperties()
font.set_weight(weight)
fig.text(0.7, yp[k], weight, fontproperties=font, **alignment)
# Show size options
sizes = [
'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large']
fig.text(0.9, 0.9, 'size', fontproperties=heading_font, **alignment)
for k, size in enumerate(sizes):
font = FontProperties()
font.set_size(size)
fig.text(0.9, yp[k], size, fontproperties=font, **alignment)
# Show bold italic
font = FontProperties(style='italic', weight='bold', size='x-small')
fig.text(0.3, 0.1, 'bold italic', fontproperties=font, **alignment)
font = FontProperties(style='italic', weight='bold', size='medium')
fig.text(0.3, 0.2, 'bold italic', fontproperties=font, **alignment)
font = FontProperties(style='italic', weight='bold', size='x-large')
fig.text(0.3, 0.3, 'bold italic', fontproperties=font, **alignment)
plt.show()