Changing font properties helps make your charts easier to read and look nicer.
Font properties customization in Matplotlib
import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties font = FontProperties(family='serif', style='italic', weight='bold', size=14, color='blue') plt.title('My Title', fontproperties=font) plt.xlabel('X axis', fontproperties=font) plt.ylabel('Y axis', fontproperties=font) plt.show()
You create a FontProperties object to set font details.
Pass this object to text elements like title, xlabel, ylabel using fontproperties.
font = FontProperties(family='serif', size=16, weight='bold') plt.title('Bold Serif Title', fontproperties=font)
font = FontProperties(family='sans-serif', style='italic', size=12) plt.xlabel('Italic X Label', fontproperties=font)
font = FontProperties(family='monospace', size=10, weight='light') plt.ylabel('Light Monospace Y Label', fontproperties=font)
This program plots a simple line chart. The title uses a serif italic bold font in dark red color. The axis labels use a green sans-serif font with normal weight.
import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties # Create font properties for title title_font = FontProperties(family='serif', style='italic', weight='bold', size=18, color='darkred') # Create font properties for axis labels label_font = FontProperties(family='sans-serif', size=14, weight='normal', color='green') # Sample data x = [1, 2, 3, 4] y = [10, 20, 25, 30] plt.plot(x, y) plt.title('Sales Over Time', fontproperties=title_font) plt.xlabel('Month', fontproperties=label_font) plt.ylabel('Sales', fontproperties=label_font) plt.show()
Not all fonts are available on every computer; if a font family is missing, matplotlib uses a default font.
You can also set font properties directly using keyword arguments like fontsize, fontweight, and color without FontProperties.
Use plt.rcParams to set default font styles for all plots.
Font properties control how text looks in your plots.
Use FontProperties to customize font family, style, weight, size, and color.
Apply font properties to titles, labels, and other text elements for better visuals.
Practice
Solution
Step 1: Understand font properties
Font properties include size, weight, family, style, and color.Step 2: Identify the property for thickness
The thickness or boldness of text is controlled by the 'weight' property.Final Answer:
weight -> Option CQuick Check:
Font thickness = weight [OK]
- Confusing size with weight
- Thinking color controls thickness
- Mixing family with weight
fontdict in matplotlib?Solution
Step 1: Recall fontdict usage
In matplotlib, fontdict keys include 'size' for font size, not 'fontsize'.Step 2: Check correct syntax
Using fontdict={'size':16} correctly sets font size to 16.Final Answer:
plt.title('My Title', fontdict={'size':16}) -> Option BQuick Check:
fontdict uses 'size' key [OK]
- Using 'fontsize' instead of 'size'
- Using 'font' instead of 'fontdict'
- Wrong dictionary key names
import matplotlib.pyplot as plt
plt.xlabel('X Axis', fontdict={'size':20, 'weight':'bold'})
plt.show()Solution
Step 1: Analyze fontdict in xlabel
The fontdict sets 'size' to 20 and 'weight' to 'bold'.Step 2: Understand effect on label
The label will use size 20 and bold weight as specified.Final Answer:
20 -> Option AQuick Check:
fontdict size = 20 [OK]
- Assuming default size
- Ignoring weight property
- Thinking code causes error
import matplotlib.pyplot as plt
plt.plot([1,2,3], label='Line')
plt.legend(prop={'style':'italic'})
plt.show()Solution
Step 1: Check font property keys for legend
Valid keys include 'size', 'weight', 'family', 'style', and 'color'. 'style' is valid but must be a recognized value.Step 2: Verify 'italic' usage
'italic' is a valid style, so the key is correct. However, in matplotlib, 'style' should be set inside a FontProperties object or as a string, not directly in prop dict.Step 3: Identify actual error
The error is that 'prop' expects a FontProperties object or a dictionary with correct keys, but 'style' key is not always recognized directly in prop dict in some matplotlib versions.Final Answer:
'style' is not a valid font property key for legend prop -> Option DQuick Check:
Legend prop keys must be valid font properties [OK]
- Using wrong keys in prop dict
- Forgetting to import FontProperties
- Assuming all keys work directly
Solution
Step 1: Check correct fontdict keys
Valid keys are 'family', 'size', 'weight', and 'color'. 'fontsize' and 'fontweight' are incorrect keys here.Step 2: Verify values and syntax
plt.title('My Plot', fontdict={'family':'Comic Sans MS', 'size':18, 'weight':'bold', 'color':'blue'}) uses correct keys and values for family, size, weight, and color.Final Answer:
plt.title('My Plot', fontdict={'family':'Comic Sans MS', 'size':18, 'weight':'bold', 'color':'blue'}) -> Option AQuick Check:
Use 'family', 'size', 'weight', 'color' keys in fontdict [OK]
- Using 'fontsize' instead of 'size'
- Using 'fontcolor' instead of 'color'
- Passing fontdict as 'font'
Ready to prove it?
Pass the test and this pattern moves unknown
