how to make subplots more distant in matplotlib figure in python

We can use the subplots_adjust() method to adjust the spacing between subplots. The method takes optional arguments left, bottom, right, and top to set the size of the margins. We can also use the hspace and wspace arguments to adjust the horizontal and vertical spaces between subplots.

Here is an example:

main.py
import matplotlib.pyplot as plt
import numpy as np

# Generate sample data
x = np.linspace(0, 2*np.pi, 100)
y1 = np.sin(x)
y2 = np.cos(x)

# Create figure and subplots
fig, ax = plt.subplots(nrows=2, ncols=1, sharex=True)

# Plot data
ax[0].plot(x, y1)
ax[1].plot(x, y2)

# Adjust spacing
fig.subplots_adjust(hspace=0.5)

# Show plot
plt.show()
345 chars
21 lines

In the example above, we create a figure with two subplots using plt.subplots(). We then plot some sample data on each subplot. Finally, we use fig.subplots_adjust() to adjust the vertical space between the subplots.

Feel free to experiment with the left, bottom, right, and top arguments, as well as the hspace and wspace arguments to get the desired spacing between the subplots in your plot.

related categories

gistlibby LogSnag