plt.subplots bigger spaces between spots in python

To create bigger spaces between subplots in Python using matplotlib.pyplot.subplots, you can use the hspace and wspace parameters. hspace controls the height spacing between subplots, while wspace controls the width spacing.

Here's an example:

main.py
import matplotlib.pyplot as plt

# Creating subplots with 2 rows and 2 columns
fig, axes = plt.subplots(2, 2)

# Adjusting the spacing between subplots
fig.subplots_adjust(hspace=0.3, wspace=0.3)

# Plotting data on the subplots
axes[0, 0].plot([1, 2, 3], [1, 2, 3])
axes[0, 1].plot([1, 2, 3], [3, 2, 1])
axes[1, 0].plot([1, 2, 3], [2, 3, 1])
axes[1, 1].plot([1, 2, 3], [1, 3, 2])

# Displaying the plot
plt.show()
415 chars
17 lines

In this example, hspace=0.3 and wspace=0.3 creates a spacing of 0.3 times the height and width of the subplot, respectively. You can adjust these values to increase or decrease the spacing as desired.

Note that the subplots_adjust() function should be called after creating the subplots but before displaying the plot using plt.show().

related categories

gistlibby LogSnag