Matplotlib subplots_adjust

Matplotlib subplots_adjust

When creating multiple plots in Matplotlib, it is often necessary to adjust the spacing between these plots to improve their visual appearance. Matplotlib provides the subplots_adjust function to accomplish this task. This article will explore the subplots_adjust function in detail, providing code examples to showcase its usage and the resulting plot adjustments.

What is subplots_adjust

The subplots_adjust function is used to adjust the spacing between subplots in a Matplotlib figure. It allows you to modify the left, right, top, bottom, wspace (horizontal spacing), and hspace (vertical spacing) between subplots.

By default, when multiple subplots are created using matplotlib.pyplot.subplots(), they are evenly spaced within the figure. However, in some cases, you may want to customize the spacing to achieve a better visual representation of your data.

Matplotlib subplots_adjust Basic Usage

The basic usage of the subplots_adjust function involves specifying the desired attributes such as left, right, top, bottom, wspace, and hspace as arguments to the function. The values for these attributes range from 0.0 to 1.0, where 0.0 represents 0% and 1.0 represents 100%.

Here is an example that demonstrates the basic usage of subplots_adjust:

import matplotlib.pyplot as plt

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

plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1, wspace=0.2, hspace=0.3)

plt.show()

This code creates a figure with a 2×2 grid of subplots and then adjusts the spacing using the subplots_adjust function. The left, right, top, and bottom attributes specify the margins around the subplots, while wspace and hspace determine the spacing between subplots.

Matplotlib subplots_adjust

Matplotlib subplots_adjust Code Examples

To further illustrate the usage of subplots_adjust, let’s dive into some code examples. We will create various plots and apply different adjustments to observe the impact on the final figure.

Example 1: Default Spacing

import matplotlib.pyplot as plt

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

plt.show()

Output:

Matplotlib subplots_adjust

The code above creates a 2×2 grid of subplots with default spacing. The resulting figure evenly spaces out the subplots within the figure area.

Example 2: Reduce Height Spacing

import matplotlib.pyplot as plt

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

plt.subplots_adjust(hspace=0.1)

plt.show()

Output:

Matplotlib subplots_adjust

In this example, we reduce the vertical spacing between the subplots by adjusting the hspace attribute. The resulting figure has less distance between the rows of subplots.

Example 3: Increase Top Margin

import matplotlib.pyplot as plt

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

plt.subplots_adjust(top=0.8)

plt.show()

Output:

Matplotlib subplots_adjust

Here, we increase the top margin of the subplots by modifying the top attribute. This change pushes the subplots downwards, allowing more space at the top.

Example 4: Remove Right Margin

import matplotlib.pyplot as plt

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

plt.subplots_adjust(right=0.95)

plt.show()

Output:

Matplotlib subplots_adjust

In this example, we eliminate the right margin of the subplots by setting the right attribute to a value closer to 1.0. This adjustment increases the available space for the subplots within the figure.

Example 5: Adjust All Margins

import matplotlib.pyplot as plt

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

plt.subplots_adjust(left=0.1, right=0.9, top=0.8, bottom=0.2)

plt.show()

Output:

Matplotlib subplots_adjust

Here, we adjust all margins of the subplots to specified values using the subplots_adjust function. This customization alters the spacing and alignment of the subplots within the figure.

Example 6: Increase Horizontal Spacing

import matplotlib.pyplot as plt

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

plt.subplots_adjust(wspace=0.6)

plt.show()

Output:

Matplotlib subplots_adjust

In this example, we increase the horizontal spacing between the subplots by adjusting the wspace attribute. The resulting figure has more space between the columns of subplots.

Example 7: Adjust Top and Bottom Spacing

import matplotlib.pyplot as plt

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

plt.subplots_adjust(top=0.8, bottom=0.2)

plt.show()

Output:

Matplotlib subplots_adjust

Here, we adjust both the top and bottom spacing of the subplots. This modification changes the aspect ratio of the subplots within the figure.

Example 8: Increase Overall Size

import matplotlib.pyplot as plt

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

plt.subplots_adjust(left=0.1, right=0.9, top=0.8, bottom=0.2, wspace=0.4, hspace=0.5)

fig.set_size_inches(10, 8)

plt.show()

Output:

Matplotlib subplots_adjust

In this example, we not only adjust the spacing but also increase the overall size of the figure using the fig.set_size_inches method. This change accommodates the adjusted spacing and enlarges the figure.

Example 9: Remove Horizontal Spacing

import matplotlib.pyplot as plt

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

plt.subplots_adjust(wspace=0)

plt.show()

Output:

Matplotlib subplots_adjust

Here, we remove the horizontal spacing between the subplots by setting the wspace attribute to 0. The resulting figure has no space between the columns of subplots.

Example 10: Advanced Spacing Adjustment

import matplotlib.pyplot as plt

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

plt.subplots_adjust(left=0.15, right=0.95, top=0.92, bottom=0.08, wspace=0.3, hspace=0.2)

plt.show()

Output:

Matplotlib subplots_adjust

In this example, we combine multiple adjustments to set specific values for each margin and the spacing between subplots. The resulting figure exhibits a complex arrangement and spacing.

Matplotlib subplots_adjust Conclusion

The subplots_adjust function in Matplotlib allows for precise customization of the spacing between subplots within a figure. By adjusting the left, right, top, bottom, wspace, and hspace attributes, you can achieve visually appealing plots. This article presented code examples showcasing different adjustments and their resulting outputs. Experiment with different values and combinations to find the ideal plot arrangement for your specific needs.

Like(0)