How To Adjust Position of Axis Labels in Matplotlib?
Matplotlib is a powerful Python library for creating graphs and charts. By default, it places axis labels in standard positions, but sometimes you might want to move them for better readability or design. This article explains easy ways to adjust the position of axis labels in Matplotlib to make your plots look better.
Adjusting axis label position using labelpad
labelpad parameter in set_xlabel() and set_ylabel() allows you to control the distance between the axis and its corresponding label. This method is useful when labels are too close to the axis and need more spacing.
import matplotlib.pyplot as plt
# Create sample plot
fig, ax = plt.subplots()
ax.plot([0, 1, 2], [0, 1, 4])
# Adjust label position using labelpad
ax.set_xlabel("X-Axis Label", labelpad=20) # Increase distance from axis
ax.set_ylabel("Y-Axis Label", labelpad=30)
plt.show()
Output

Explanation: labelpad in ax.set_xlabel() and ax.set_ylabel() controls the label’s distance from the axis. Higher values increase spacing, improving readability and preventing overlap. Here, labelpad=20 for the x-axis and labelpad=30 for the y-axis enhance clarity.
Manually adjusting label postition using set_position()
If you need explicit control over label positioning, you can use the set_position() method. This method allows you to specify the exact coordinates of the axis labels.
fig, ax = plt.subplots()
ax.plot([0, 1, 2], [0, 1, 4])
# Adjust label position using set_position()
ax.xaxis.label.set_position((0.5, -0.1)) # (x, y) coordinates
ax.yaxis.label.set_position((-0.2, 0.5))
plt.xlabel("X-Axis Label")
plt.ylabel("Y-Axis Label")
plt.show()
Output

Explanation: set_position() in ax.xaxis.label and ax.yaxis.label sets label coordinates relative to the axes. (0.5, -0.1) centers the x-axis label lower, while (-0.2, 0.5) shifts the y-axis label left, keeping it vertically centered.
Rotating axis labels for better visibility
When working with long labels or a crowded layout, rotating axis labels can improve readability. You can use the set_rotation() method to rotate labels at a specified angle.
fig, ax = plt.subplots()
ax.plot([0, 1, 2], [0, 1, 4])
# Rotate labels
ax.set_xlabel("X-Axis Label", labelpad=10)
ax.set_ylabel("Y-Axis Label", labelpad=10)
ax.xaxis.label.set_rotation(30) # Rotate x-axis label
ax.yaxis.label.set_rotation(90) # Rotate y-axis label
plt.show()
Output

Explanation: set_rotation() in ax.xaxis.label and ax.yaxis.label rotates axis labels for better readability. Here, 30° tilts the x-axis label diagonally, while 90° rotates the y-axis label vertically. This helps prevent overlap and improves clarity in dense plots.
Adjusting axis labels with subplots
When dealing with multiple subplots, ensuring proper spacing of axis labels is essential. Using tight_layout() helps prevent overlapping labels.
import matplotlib.pyplot as plt
# Create a 2x2 grid of subplots with figure size 8x6
fig, axs = plt.subplots(2, 2, figsize=(8, 6))
# Loop through each subplot
for ax in axs.flat:
ax.plot([0, 1, 2], [0, 1, 4])
ax.set_xlabel("X-Axis", labelpad=15) # Set x-axis label with padding
ax.set_ylabel("Y-Axis", labelpad=15) # Set y-axis label with padding
plt.tight_layout()
plt.show()
Output

Explanation: plt.subplots(2, 2, figsize=(8, 6)) creates a 2×2 grid of subplots. The loop iterates through each subplot (axs.flat), plotting a line and setting axis labels with labelpad=15 for spacing. plt.tight_layout() adjusts spacing to prevent overlap, ensuring a clear layout.