Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manipulation on vertical space in matplotlib subplots

Tags:

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.

like image 881
user2820579 Avatar asked Mar 02 '16 17:03

user2820579


People also ask

How do I increase the vertical space between subplots in Matplotlib?

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.

How do you resize a PLT subplot?

To change figure size of more subplots you can use plt. subplots(2,2,figsize=(10,10)) when creating subplots.

What does PLT subplot 1 2 1 mean?

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.

How do I stop subplots from overlapping in Matplotlib?

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.


1 Answers

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 enter image description here

Using 0.2 as the value of wspace gives

enter image description here

like image 147
DavidG Avatar answered Sep 23 '22 14:09

DavidG