WikiGalaxy

Personalize

Sharing Axes Across Subplots

Introduction to Sharing Axes

Sharing axes across subplots in Matplotlib is a powerful feature that allows for synchronized zooming and panning of plots. This is particularly useful when comparing data across multiple plots, as it ensures consistency in axis scaling and makes differences more apparent.

Benefits of Sharing Axes

When axes are shared, any change in the view limits of one subplot will automatically be applied to the others. This is beneficial for comparative analysis, ensuring that the viewer can focus on data trends rather than adjusting axes manually.

Sharing X-Axis

Sharing X-Axis Across Subplots

By sharing the x-axis, you ensure that all subplots have the same x-axis limits, which is helpful when the x-axis represents a common variable like time or categories.


import matplotlib.pyplot as plt

fig, (ax1, ax2) = plt.subplots(2, sharex=True)
ax1.plot([1, 2, 3], [1, 4, 9])
ax2.plot([1, 2, 3], [9, 4, 1])

plt.show()
        

Explanation

In this example, two subplots share the x-axis. The x-axis limits and ticks are synchronized, making it easier to compare the y-values across the two plots.

Sharing Y-Axis

Sharing Y-Axis Across Subplots

Sharing the y-axis ensures that all subplots have the same y-axis limits, which is crucial when comparing datasets with similar y-values.


import matplotlib.pyplot as plt

fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.plot([1, 2, 3], [1, 4, 9])
ax2.plot([1, 2, 3], [1, 2, 3])

plt.show()
        

Explanation

Here, the y-axis is shared between two subplots. This is particularly useful for comparing the shape and trend of two datasets with a common y-scale.

Sharing Both Axes

Full Axis Sharing

Sharing both axes across subplots is useful when you need to maintain a consistent scale across multiple plots, helping to highlight differences and similarities.


import matplotlib.pyplot as plt

fig, axs = plt.subplots(2, 2, sharex=True, sharey=True)
axs[0, 0].plot([1, 2, 3], [1, 4, 9])
axs[0, 1].plot([1, 2, 3], [9, 4, 1])
axs[1, 0].plot([1, 2, 3], [1, 2, 3])
axs[1, 1].plot([1, 2, 3], [3, 2, 1])

plt.show()
        

Explanation

In this example, both the x and y axes are shared across a 2x2 grid of subplots. This setup is ideal for comparing multiple datasets with similar ranges.

Independent Axes with Shared Axes

Combining Shared and Independent Axes

Sometimes, you may want to share axes only across specific subplots while keeping others independent. This can be achieved by selectively sharing axes.


import matplotlib.pyplot as plt

fig, axs = plt.subplots(2, 2, sharex='col')
axs[0, 0].plot([1, 2, 3], [1, 4, 9])
axs[0, 1].plot([1, 2, 3], [9, 4, 1])
axs[1, 0].plot([1, 2, 3], [1, 2, 3])
axs[1, 1].plot([1, 2, 3], [3, 2, 1])

plt.show()
        

Explanation

In this case, the x-axis is shared across columns, allowing for comparison within each column while maintaining independent y-axis scales.

Sharing Axes in Complex Layouts

Advanced Axis Sharing

In complex subplot layouts, sharing axes can help maintain a coherent visual structure, making the data presentation more intuitive.


import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222, sharey=ax1)
ax3 = fig.add_subplot(223, sharex=ax1)
ax4 = fig.add_subplot(224)

ax1.plot([1, 2, 3], [1, 4, 9])
ax2.plot([1, 2, 3], [9, 4, 1])
ax3.plot([1, 2, 3], [1, 2, 3])
ax4.plot([1, 2, 3], [3, 2, 1])

plt.show()
        

Explanation

This example demonstrates how to share axes in a more complex layout using the `add_subplot` method. Axes are shared selectively to enhance data comparison while maintaining individual plot characteristics.

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025