0
0
MatplotlibHow-ToBeginner ยท 3 min read

How to Set Font Size in Matplotlib: Simple Guide

You can set font size in Matplotlib by using the fontsize parameter in functions like plt.title(), plt.xlabel(), and plt.ylabel(). For tick labels, use plt.xticks(fontsize=) and plt.yticks(fontsize=). You can also set a global font size using plt.rcParams['font.size'].
๐Ÿ“

Syntax

Here are common ways to set font size in Matplotlib:

  • plt.title('Title', fontsize=12): Sets font size of the plot title.
  • plt.xlabel('X-axis', fontsize=10): Sets font size of the x-axis label.
  • plt.ylabel('Y-axis', fontsize=10): Sets font size of the y-axis label.
  • plt.xticks(fontsize=8): Sets font size of x-axis tick labels.
  • plt.yticks(fontsize=8): Sets font size of y-axis tick labels.
  • plt.rcParams['font.size'] = 14: Sets default font size globally for all text.
python
import matplotlib.pyplot as plt

plt.rcParams['font.size'] = 14  # global font size

plt.title('Title', fontsize=12)
plt.xlabel('X-axis', fontsize=10)
plt.ylabel('Y-axis', fontsize=10)
plt.xticks(fontsize=8)
plt.yticks(fontsize=8)
๐Ÿ’ป

Example

This example shows how to set font sizes for title, axis labels, and tick labels in a simple line plot.

python
import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [10, 20, 25, 30]

plt.plot(x, y)
plt.title('Sample Plot', fontsize=16)
plt.xlabel('X Axis Label', fontsize=14)
plt.ylabel('Y Axis Label', fontsize=14)
plt.xticks(fontsize=12)
plt.yticks(fontsize=12)

plt.show()
Output
A line plot with a large title, axis labels, and tick labels all showing different font sizes as set.
โš ๏ธ

Common Pitfalls

Common mistakes when setting font size in Matplotlib include:

  • Forgetting to use the fontsize parameter inside text functions like plt.title().
  • Trying to set font size directly on tick labels without using plt.xticks() or plt.yticks().
  • Setting plt.rcParams['font.size'] after plotting, which won't affect existing text.
  • Confusing size with fontsize parameter names (always use fontsize).

Example of wrong and right ways:

python
import matplotlib.pyplot as plt

x = [1, 2, 3]
y = [4, 5, 6]

plt.plot(x, y)
# Wrong: font size ignored
plt.title('Wrong Title', size=20)  # 'size' is not recognized

# Right:
plt.title('Right Title', fontsize=20)
plt.show()
Output
Plot showing the title with correct font size only for the right example.
๐Ÿ“Š

Quick Reference

Function/ParameterPurposeExample
plt.title()Set title text and font sizeplt.title('Title', fontsize=16)
plt.xlabel()Set x-axis label and font sizeplt.xlabel('X Label', fontsize=12)
plt.ylabel()Set y-axis label and font sizeplt.ylabel('Y Label', fontsize=12)
plt.xticks()Set font size of x-axis tick labelsplt.xticks(fontsize=10)
plt.yticks()Set font size of y-axis tick labelsplt.yticks(fontsize=10)
plt.rcParams['font.size']Set global default font sizeplt.rcParams['font.size'] = 14
โœ…

Key Takeaways

Use the fontsize parameter inside text functions like plt.title() to set font size.
Set tick label font size with plt.xticks(fontsize=) and plt.yticks(fontsize=).
Use plt.rcParams['font.size'] to set a global font size before plotting.
Avoid using incorrect parameter names like size instead of fontsize.
Set font sizes before calling plt.show() to see the changes.