Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Show tick labels when sharing an axis in Matplotlib
When creating subplots that share an axis in Matplotlib, you might want to show tick labels on all subplots instead of hiding them on shared axes. By default, Matplotlib hides tick labels on shared axes to avoid redundancy, but you can control this behavior.
Default Behavior with Shared Y-Axis
When using sharey parameter, Matplotlib automatically hides y-axis tick labels on the right subplot to avoid duplication ?
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [10, 4]
plt.rcParams["figure.autolayout"] = True
# Create first subplot
ax1 = plt.subplot(1, 2, 1)
ax1.plot([1, 4, 9])
ax1.set_title('Left Plot')
# Create second subplot sharing y-axis
ax2 = plt.subplot(1, 2, 2, sharey=ax1)
ax2.plot([1, 8, 27])
ax2.set_title('Right Plot (shared y-axis)')
plt.show()
Showing Tick Labels on Shared Axis
To show tick labels on both subplots when sharing an axis, use tick_params() method with labelleft=True or labelright=True ?
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [10, 4]
plt.rcParams["figure.autolayout"] = True
# Create subplots with shared y-axis
ax1 = plt.subplot(1, 2, 1)
ax1.plot([1, 4, 9], 'b-', label='Subplot 1')
ax1.set_title('Left Plot')
ax2 = plt.subplot(1, 2, 2, sharey=ax1)
ax2.plot([1, 8, 27], 'r-', label='Subplot 2')
ax2.set_title('Right Plot')
# Show tick labels on the right subplot
ax2.tick_params(axis='y', labelleft=True)
plt.show()
Alternative Method Using setp()
You can also use setp() to make tick labels visible ?
import matplotlib.pyplot as plt
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4), sharey=True)
# Plot data on both axes
ax1.plot([1, 4, 9, 16], 'bo-')
ax1.set_title('Squares')
ax2.plot([1, 8, 27, 64], 'ro-')
ax2.set_title('Cubes')
# Make y-axis labels visible on right subplot
plt.setp(ax2.get_yticklabels(), visible=True)
plt.tight_layout()
plt.show()
Controlling X-Axis Labels
Similar control applies to x-axis when using sharex parameter ?
import matplotlib.pyplot as plt
import numpy as np
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(8, 6), sharex=True)
x = np.linspace(0, 10, 50)
# Plot on both subplots
ax1.plot(x, np.sin(x))
ax1.set_title('Sine Wave')
ax2.plot(x, np.cos(x))
ax2.set_title('Cosine Wave')
# Show x-axis labels on top subplot
ax1.tick_params(axis='x', labelbottom=True)
plt.tight_layout()
plt.show()
Summary
| Method | Usage | Best For |
|---|---|---|
tick_params(labelleft=True) |
Enable y-axis labels on shared axis | Y-axis sharing |
tick_params(labelbottom=True) |
Enable x-axis labels on shared axis | X-axis sharing |
setp(get_yticklabels(), visible=True) |
Alternative method for visibility | Complex customizations |
Conclusion
Use tick_params() with appropriate label parameters to show tick labels on shared axes. This gives you full control over which subplots display tick labels while maintaining axis synchronization.
