I want to reduce the verticalspacing between subplot. Surfing along the web I just have found how to reduce the horizontal spacing, something like
import matplotlib.pyplot as plt fig, axes = plt.subplots(nrows=4, ncols=4) fig.tight_layout() # Or equivalently, "plt.tight_layout()" fig.subplots_adjust(hspace=0.5) plt.show()
The hspace thing is the one that manipulates such behaviour, but apparently there's no vspace.
EDIT:
This does not reduce the space between the y-axis, that is what I want to manipulate.
We can use the plt. subplots_adjust() method to change the space between Matplotlib subplots. The parameters wspace and hspace specify the space reserved between Matplotlib subplots. They are the fractions of axis width and height, respectively.
To change figure size of more subplots you can use plt. subplots(2,2,figsize=(10,10)) when creating subplots.
The subplot() Function The layout is organized in rows and columns, which are represented by the first and second argument. The third argument represents the index of the current plot. plt.subplot(1, 2, 1) #the figure has 1 row, 2 columns, and this plot is the first plot.
Often you may use subplots to display multiple plots alongside each other in Matplotlib. Unfortunately, these subplots tend to overlap each other by default. The easiest way to resolve this issue is by using the Matplotlib tight_layout() function.
As you said in your question hspace
reduces the vertical spacing between subplots. The equivalent for horizontal spacing between subplots is wspace
. Below is an example:
x = np.linspace(0, 2 * np.pi, 400) y = np.sin(x ** 2) fig, ((ax1,ax2),(ax3,ax4)) = plt.subplots(nrows=2, ncols=2) fig.tight_layout() ax1.plot(x, y) ax2.scatter(x, y) ax3.scatter(x, y) ax4.scatter(x, y) fig.subplots_adjust(wspace=0.2) plt.show()
Using a value for 1 for wspace
gives
Using 0.2 as the value of wspace
gives
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With