Customizing Font Size in Matplotlib

Creating well-designed plots in matplotlib requires effectively adjusting the font size of various elements to make the plots easy to read. This tutorial will guide you through the process of customizing font sizes in various parts of your plots using the `matplotlib` library.

We'll start with basic font size adjustments and move on to more advanced customizations and examples.

### Basic Font Size for Titles and Labels

Here's a simple example to adjust the font size for plot titles and axis labels:

import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]

plt.plot(x, y)

# Customize font sizes
plt.title('Plot Title', fontsize=20)
plt.xlabel('X-axis Label', fontsize=15)
plt.ylabel('Y-axis Label', fontsize=15)

plt.show()
- **`plt.title('Plot Title', fontsize=20)`** sets the title of the plot with a font size of 20.
- **`plt.xlabel('X-axis Label', fontsize=15)`** and **`plt.ylabel('Y-axis Label', fontsize=15)`** set the labels of the x-axis and y-axis with a font size of 15.

### Customizing Tick Labels

Tick labels can also be customized to improve the readability of the plot:

import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]

plt.plot(x, y)

plt.title('Plot Title', fontsize=20)
plt.xlabel('X-axis Label', fontsize=15)
plt.ylabel('Y-axis Label', fontsize=15)

# Customize tick labels
plt.xticks(fontsize=12)
plt.yticks(fontsize=12)

plt.show()
- **`plt.xticks(fontsize=12)`** and **`plt.yticks(fontsize=12)`** set the font size of the x-axis and y-axis tick labels to 12.

### Customizing Legends

You can also adjust the font size of legends to make them more readable:

import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4, 5]
y1 = [1, 4, 9, 16, 25]
y2 = [2, 4, 6, 8, 10]

plt.plot(x, y1, label='Quadratic')
plt.plot(x, y2, label='Linear')

plt.title('Plot Title', fontsize=20)
plt.xlabel('X-axis Label', fontsize=15)
plt.ylabel('Y-axis Label', fontsize=15)

# Customize legends
plt.legend(fontsize=10)

plt.show()
- **`plt.legend(fontsize=10)`** sets the font size of the legend to 10.

### Applying Custom Font Sizes Globally

To apply custom font sizes globally across the entire plot, you can use `matplotlib`'s `rcParams`:

import matplotlib.pyplot as plt

# Apply global font size
plt.rcParams.update({'font.size': 14})

# Sample data
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]

plt.plot(x, y)

plt.title('Plot Title')
plt.xlabel('X-axis Label')
plt.ylabel('Y-axis Label')

plt.show()
- **`plt.rcParams.update({'font.size': 14})`** globally sets the font size to 14 for all elements in the plot.

### Advanced Customizations

You can also customize font properties, such as weight, style, and family:

import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]

plt.plot(x, y)

# Customize font properties
plt.title('Plot Title', fontsize=20, fontweight='bold', family='serif')
plt.xlabel('X-axis Label', fontsize=15, style='italic')
plt.ylabel('Y-axis Label', fontsize=15, weight='light')

plt.show()
- **`fontweight='bold'`** makes the title font bold.
- **`style='italic'`** makes the x-axis label font italic.
- **`weight='light'`** sets a lighter weight for the y-axis label.

### Example with Combined Customizations

Here's an example combining the various customizations to create a polished and readable plot:

import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4, 5]
y1 = [1, 4, 9, 16, 25]
y2 = [2, 4, 6, 8, 10]

plt.plot(x, y1, label='Quadratic')
plt.plot(x, y2, label='Linear')

# Customize title and labels
plt.title('Customized Plot', fontsize=22, fontweight='bold', family='serif')
plt.xlabel('X-axis', fontsize=18, style='italic')
plt.ylabel('Y-axis', fontsize=18, weight='medium')

# Customize tick labels
plt.xticks(fontsize=12)
plt.yticks(fontsize=12)

# Customize legend
plt.legend(fontsize=14)

plt.show()