What is the purpose of font properties in matplotlib?
Font properties control the appearance of text in plots, such as font family, size, weight, and style, to make the text clear and visually appealing.
Click to reveal answer
beginner
How do you change the font size of a title in matplotlib?
Use the fontsize parameter in the title() function, for example: plt.title('My Title', fontsize=14).
Click to reveal answer
beginner
What does the fontweight property control?
The fontweight property controls how thick or bold the text appears. Common values are 'normal', 'bold', and numeric weights like 700.
Click to reveal answer
intermediate
How can you set multiple font properties at once in matplotlib?
You can create a dictionary with font properties like {'family': 'serif', 'size': 12, 'weight': 'bold'} and pass it using the fontdict parameter, e.g., plt.xlabel('X', fontdict=font).
Click to reveal answer
intermediate
What is the difference between fontfamily and fontname in matplotlib?
fontfamily specifies a generic font category like 'serif' or 'sans-serif', while fontname specifies a specific font like 'Arial' or 'Times New Roman'.
Click to reveal answer
Which parameter changes the font size of axis labels in matplotlib?
Afontstyle
Bfontweight
Cfontcolor
Dfontsize
✗ Incorrect
The fontsize parameter controls the size of the font for axis labels.
How do you make text bold in matplotlib?
ASet fontweight='bold'
BSet fontsize=20
CSet fontstyle='italic'
DSet fontfamily='serif'
✗ Incorrect
The fontweight property set to 'bold' makes text bold.
What does the fontdict parameter do?
ASets multiple font properties at once
BChanges the plot background color
CAdjusts the plot size
DAdds grid lines
✗ Incorrect
fontdict lets you pass a dictionary of font properties to style text.
Which font property controls the font family category?
Afontsize
Bfontfamily
Cfontweight
Dfontstyle
✗ Incorrect
fontfamily sets the generic font category like serif or sans-serif.
If you want italic text, which property should you use?
Afontweight='italic'
Bfontsize=italic
Cfontstyle='italic'
Dfontfamily='italic'
✗ Incorrect
The fontstyle property set to 'italic' makes text italic.
Explain how to customize font properties for a plot title in matplotlib.
Think about parameters in plt.title() and how to pass a dictionary of font properties.
You got /5 concepts.
Describe the difference between fontfamily and fontname in matplotlib font customization.
Consider how you choose a broad font type versus a specific font face.
You got /3 concepts.
Practice
(1/5)
1. Which font property controls the thickness of the text in a matplotlib plot?
easy
A. family
B. size
C. weight
D. color
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 C
Quick Check:
Font thickness = weight [OK]
Hint: Thickness means weight in font properties [OK]
Common Mistakes:
Confusing size with weight
Thinking color controls thickness
Mixing family with weight
2. Which of the following is the correct way to set the font size of a plot title to 16 using fontdict in matplotlib?
easy
A. plt.title('My Title', font={'fontsize':16})
B. plt.title('My Title', fontdict={'size':16})
C. plt.title('My Title', font={'size':16})
D. plt.title('My Title', fontdict={'fontsize':16})
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 B
Quick Check:
fontdict uses 'size' key [OK]
Hint: Use 'size' key inside fontdict for font size [OK]
Common Mistakes:
Using 'fontsize' instead of 'size'
Using 'font' instead of 'fontdict'
Wrong dictionary key names
3. What will be the font size of the x-axis label in the following code?
import matplotlib.pyplot as plt
plt.xlabel('X Axis', fontdict={'size':20, 'weight':'bold'})
plt.show()
medium
A. 20
B. Bold but default size
C. Default size (usually 10)
D. Error due to wrong fontdict
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 A
Quick Check:
fontdict size = 20 [OK]
Hint: fontdict size sets label font size directly [OK]
Common Mistakes:
Assuming default size
Ignoring weight property
Thinking code causes error
4. Identify the error in this code snippet that tries to set the legend font to italic:
import matplotlib.pyplot as plt
plt.plot([1,2,3], label='Line')
plt.legend(prop={'style':'italic'})
plt.show()
medium
A. Missing plt.legend() call
B. No error, code runs fine
C. Incorrect label argument in plot
D. 'style' is not a valid font property key for legend prop
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 D
Quick Check:
Legend prop keys must be valid font properties [OK]
Hint: Use FontProperties for complex font styles in legend [OK]
Common Mistakes:
Using wrong keys in prop dict
Forgetting to import FontProperties
Assuming all keys work directly
5. You want to create a plot with a title that uses a custom font family 'Comic Sans MS', size 18, bold weight, and blue color. Which code snippet correctly applies all these font properties?
hard
A. plt.title('My Plot', fontdict={'family':'Comic Sans MS', 'size':18, 'weight':'bold', 'color':'blue'})
B. plt.title('My Plot', fontdict={'fontfamily':'Comic Sans MS', 'fontsize':18, 'fontweight':'bold', 'fontcolor':'blue'})
C. plt.title('My Plot', font={'family':'Comic Sans MS', 'size':18, 'weight':'bold', 'color':'blue'})
D. plt.title('My Plot', fontdict={'family':'Comic Sans MS', 'fontsize':18, 'weight':'bold', 'color':'blue'})
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 A
Quick Check:
Use 'family', 'size', 'weight', 'color' keys in fontdict [OK]
Hint: Use standard keys: family, size, weight, color in fontdict [OK]
Common Mistakes:
Using 'fontsize' instead of 'size'
Using 'fontcolor' instead of 'color'
Passing fontdict as 'font'
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.