Matplotlib Axis Labels

In this tutorial, you'll learn how to add and customize axis labels using Matplotlib in Python. Axis labels help in clearly conveying the meaning of the data in your plots.

## Contents
1. **Basic Axis Labels**
2. **Customization of Axis Labels (Font Size, Color, and Rotation)**
3. **Using Labelpad to Adjust Spacing**
4. **Adding Labels to Subplots**
5. **Using LaTeX for Axis Labels**
6. **Rotating Axis Tick Labels**

### 1. Basic Axis Labels

To add axis labels to your Matplotlib plot, you use the `xlabel()` and `ylabel()` functions. Here's a simple example:

import matplotlib.pyplot as plt

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

plt.plot(x, y)
plt.xlabel('X Axis')  # Adding label to x-axis
plt.ylabel('Y Axis')  # Adding label to y-axis
plt.title('Basic Axis Labels')
plt.show()
### 2. Customization of Axis Labels (Font Size, Color, and Rotation)

Customizing axis labels helps to make your plots more readable and visually appealing. You can change the font size, color, and rotation of the labels.

import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

plt.plot(x, y)
plt.xlabel('X Axis', fontsize=14, color='red')  # Changing font size and color
plt.ylabel('Y Axis', fontsize=14, color='blue', rotation=45)  # Changing font size, color, and rotation
plt.title('Customized Axis Labels')
plt.show()
### 3. Using Labelpad to Adjust Spacing

The `labelpad` parameter can be used to adjust the spacing between the axis and its label.

import matplotlib.pyplot as plt

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

plt.plot(x, y)
plt.xlabel('X Axis', labelpad=20)  # Adding extra space
plt.ylabel('Y Axis', labelpad=20)  # Adding extra space
plt.title('Axis Labels with Labelpad')
plt.show()
### 4. Adding Labels to Subplots

When dealing with subplots, adding axis labels to each subplot individually can help differentiate them clearly.

import matplotlib.pyplot as plt

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

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))  # Create two subplots

ax1.plot(x, y1)
ax1.set_xlabel('X Axis 1')  # Adding label to x-axis of first subplot
ax1.set_ylabel('Y Axis 1')  # Adding label to y-axis of first subplot
ax1.set_title('Plot 1')

ax2.plot(x, y2)
ax2.set_xlabel('X Axis 2')  # Adding label to x-axis of second subplot
ax2.set_ylabel('Y Axis 2')  # Adding label to y-axis of second subplot
ax2.set_title('Plot 2')

plt.tight_layout()  # Adjust spacing to prevent overlap
plt.show()
## Using LaTeX for Axis Labels
For more advanced text formatting, you can use LaTeX commands with Matplotlib.

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y = np.sin(x)

plt.plot(x, y)
plt.xlabel(r'$\alpha$ (radians)', fontsize=14)  # LaTeX formatted X-axis label
plt.ylabel(r'$\beta$ (sin $\alpha$)', fontsize=14)  # LaTeX formatted Y-axis label

plt.show()
## Rotating Axis Tick Labels
Sometimes, especially when dealing with complex plots, you might want to rotate the axis tick labels.

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y = np.sin(x)

plt.plot(x, y)
plt.xlabel('X-axis Label', fontsize=14)
plt.ylabel('Y-axis Label', fontsize=14)

# Rotate labels by 45 degrees
plt.xticks(rotation=45)  # Rotate X-axis labels
plt.yticks(rotation=90)  # Rotate Y-axis labels

plt.show()
In this tutorial, we've explored several ways to add and customize axis labels in Matplotlib. Adding clear, descriptive, and well-formatted labels to your axes can make your plots much easier to understand and interpret.