Consider the following matplotlib code that sets font sizes for different parts of a plot. What will be the font size of the plot's title?
import matplotlib.pyplot as plt plt.figure() plt.title('Sample Title', fontsize=16) plt.xlabel('X-axis', fontsize=12) plt.ylabel('Y-axis', fontsize=12) plt.show()
Look at the fontsize argument inside the plt.title() function.
The font size for the title is explicitly set to 16 using the fontsize=16 argument in plt.title(). Other font sizes do not affect the title.
Given the code below, what will be the font size of the x-axis tick labels?
import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.plot([1, 2, 3], [4, 5, 6]) for tick in ax.get_xticklabels(): tick.set_fontsize(14) plt.show()
Check the loop where set_fontsize is called on each tick label.
The code sets the font size of each x-axis tick label to 14 explicitly using set_fontsize(14).
Look at the code below that creates a plot with a legend. What font size will the legend text have?
import matplotlib.pyplot as plt plt.plot([1, 2, 3], label='Line 1') plt.plot([3, 2, 1], label='Line 2') plt.legend(fontsize='small') plt.show()
The fontsize argument in plt.legend() can take string values like 'small'.
The legend font size is set to 'small', which corresponds to a font size of 10 in matplotlib's default settings.
Which of the following is the best reason to use relative font sizes (like 'small', 'medium', 'large') instead of fixed numeric sizes in matplotlib?
Think about how plots scale on different screens or when saved at different resolutions.
Using relative font sizes helps keep text readable and proportional when the figure size or resolution changes.
Examine the code below. The user wants to set the font size of axis labels to 14, but the labels remain small. What is the cause?
import matplotlib.pyplot as plt plt.plot([1, 2, 3], [4, 5, 6]) plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.rcParams['font.size'] = 14 plt.show()
Consider when rcParams changes take effect relative to plot commands.
Changing rcParams['font.size'] after creating labels does not update their font size. It must be set before or labels recreated.