Matplotlib Subplots Spacing

Matplotlib Subplots Spacing

When working with matplotlib, subplots are a great way to display multiple plots in a single figure. However, sometimes the default spacing between subplots is not ideal, and we might need to adjust it to improve the overall appearance of the figure. In this article, we will explore different methods to control the spacing between subplots using matplotlib.

Understanding Spacing in Subplots

Before diving into the code examples, let’s briefly understand how subplots are organized and spaced within a figure.

When we create subplots using the plt.subplots() function, we can specify the number of rows and columns. This determines the grid of subplots that matplotlib will create within the figure. Each subplot is identified by an index that ranges from 0 to (nrows * ncols) - 1, where nrows is the number of rows and ncols is the number of columns.

By default, matplotlib divides the figure evenly into equal-sized subplots, leaving equal spacing between them. The spacing between subplots is controlled by parameters like hspace (horizontal spacing) and wspace (vertical spacing), which are specified as ratios of the subplot widths and heights, respectively. When these parameters have a value of 1, the spacing is equal to the width or height of a subplot.

Now, let’s look at some code examples to visualize the effect of different spacing parameters on subplots.

Code Examples

Example 1: Default Subplot Spacing

In this example, we create a 2×2 grid of subplots with the default spacing:

import matplotlib.pyplot as plt

fig, axs = plt.subplots(2, 2)

plt.show()

Output:

Matplotlib Subplots Spacing

As we can see, the default spacing leaves equal gaps between the subplots.

Example 2: Adjusting Subplot Spacing

To adjust the subplot spacing, we can use the subplots_adjust() function provided by matplotlib. This function accepts various parameters to control spacing.

In the following example, we increase the horizontal spacing between subplots by setting wspace=0.4:

import matplotlib.pyplot as plt

fig, axs = plt.subplots(2, 2)

plt.subplots_adjust(wspace=0.4)

plt.show()

Output:

Matplotlib Subplots Spacing

By setting wspace=0.4, we have increased the horizontal spacing between the subplots. We can choose any value between 0 and 1 to control the amount of spacing.

Example 3: Adjusting Vertical Spacing

Similar to the previous example, we can adjust the vertical spacing between subplots using the hspace parameter.

In this example, we set hspace=0.6 to increase the vertical spacing:

import matplotlib.pyplot as plt

fig, axs = plt.subplots(2, 2)

plt.subplots_adjust(hspace=0.6)

plt.show()

Output:

Matplotlib Subplots Spacing

By increasing the hspace value, we have created more vertical space between the subplots.

Example 4: Customizing Spacing for Subplots

We can also customize the spacing differently for the horizontal and vertical directions by specifying separate hspace and wspace parameters.

In the following example, we set hspace=0.3 and wspace=0.6 to adjust the spacing:

import matplotlib.pyplot as plt

fig, axs = plt.subplots(2, 2)

plt.subplots_adjust(hspace=0.3, wspace=0.6)

plt.show()

Output:

Matplotlib Subplots Spacing

By setting different values for hspace and wspace, we can create custom spacing between the subplots.

Example 5: Equal Subplot Spacing

To achieve equal spacing between subplots, we can set both hspace and wspace to the same value.

In this example, we set hspace=0.5 and wspace=0.5 to create equal spacing:

import matplotlib.pyplot as plt

fig, axs = plt.subplots(2, 2)

plt.subplots_adjust(hspace=0.5, wspace=0.5)

plt.show()

Output:

Matplotlib Subplots Spacing

By setting equal values for hspace and wspace, we ensure consistent spacing between the subplots.

Example 6: Unequal Spacing between Rows and Columns

To create different spacing between rows and columns, we can set distinct values for hspace and wspace. This can be useful when we want to emphasize a particular dimension.

In the following example, we set hspace=0.2 and wspace=0.5 to create more spacing between columns:

import matplotlib.pyplot as plt

fig, axs = plt.subplots(2, 2)

plt.subplots_adjust(hspace=0.2, wspace=0.5)

plt.show()

Output:

Matplotlib Subplots Spacing

By setting a different value for wspace, we have increased the spacing between the columns.

Example 7: Fine-Tuning Spacing

Instead of specifying the spacing as a ratio, we can also use absolute values to fine-tune the subplot spacing.

In this example, we set hspace=0.3 and wspace=0.2, which results in smaller spacing than the previous examples:

import matplotlib.pyplot as plt

fig, axs = plt.subplots(2, 2)

plt.subplots_adjust(hspace=0.3, wspace=0.2)

plt.show()

Output:

Matplotlib Subplots Spacing

By using smaller values for hspace and wspace, we have reduced the spacing between the subplots.

Example 8: Spacing with Different Numbers of Subplots

The methods we have seen so far work well when we have a regular grid of subplots. However, we can also adjust the spacing when the number of rows or columns varies.

In this example, we create a 1×3 grid and set wspace=0.3 to increase the horizontal spacing:

import matplotlib.pyplot as plt

fig, axs = plt.subplots(1, 3)

plt.subplots_adjust(wspace=0.3)

plt.show()

Output:

Matplotlib Subplots Spacing

By increasing the wspace value, we have created more spacing between the subplots in the horizontal direction.

Example 9: Spacing with Single Subplot

Even when we have just a single subplot, we can still adjust the spacing. This can be useful when we want to leave some margin around the plot.

In the following example, we create a single subplot with wspace=0.4 and hspace=0.2:

import matplotlib.pyplot as plt

fig, axs = plt.subplots(1, 1)

plt.subplots_adjust(wspace=0.4, hspace=0.2)

plt.show()

Output:

Matplotlib Subplots Spacing

By setting non-zero values for wspace and hspace, we have added some margin around the single subplot.

Example 10: Subplots with Different Sizes

Finally, we can create subplots with different sizes and still adjust the spacing between them.

In this example, we create 3 subplots with varying sizes and set wspace=0.2:

import matplotlib.pyplot as plt

fig, axs = plt.subplots(3, 1, gridspec_kw={'height_ratios': [1, 2, 3]})

plt.subplots_adjust(wspace=0.2)

plt.show()

Output:

Matplotlib Subplots Spacing

By setting wspace=0.2, we have increased the spacing between the subplots in the horizontal direction, accounting for their varying sizes.

Matplotlib Subplots Spacing Conclusion

In this article, we have explored different methods to adjust the spacing between subplots using matplotlib. By manipulating the hspace and wspace parameters, we can control the spacing in both the horizontal and vertical directions. These techniques are useful when working with multiple plots within a single figure, allowing us to improve the overall appearance of our visualizations.

Like(0)