How can I increase the height (size) and space between subplots in python

Viewed 151

I want to have two equal size subplots and increase the height (size) of my third subplot. Also, my third plot sticks to my second subplot. I want to have a small distance between the second and the third subplots. I should mention that my X axis is common for all subplots. This is part of my code.

from datetime import datetime, timedelta
from matplotlib import pyplot as plt
from matplotlib import dates as mpl_dates

ax1 = plt.subplot(311)
plt.plot(date,amount, color='gray', linewidth=0.3)
plt.ylabel('2-4 Hz')

   
ax2 = plt.subplot(312)
plt.plot(date,amount, color='brown', linewidth=0.3)
plt.ylabel('0.4-2 Hz')

ax3 = plt.subplot(313)
plt.bar (date, amount, color='gold', edgecolor='red', align='center')
plt.ylabel('rainfall(mm/day)')

ax1.get_shared_x_axes().join(ax1, ax2, ax3)
plt.subplots_adjust(hspace=0.01)


plt.show()

enter image description here

1 Answers

I would approach this using the gridspec_kw argument in plt.subplots(), and then manually add a little space between the second and third axes. The gridspec_kw lets you set the height ratios of each axes.

There might be a better way to add the space between the second and third axis, but I think this meets your needs.

fig, ax = plt.subplots(3, 1, sharex=True, gridspec_kw={'height_ratios':[1,1,2]})
fig.subplots_adjust(hspace=0.0)
ax1 = ax[0]
ax2 = ax[1]
ax3 = ax[2]
bbox = list(ax3.get_position().bounds)
bbox[3] = 0.9*bbox[3] # Reduce the height of the axis a bit.
ax3.set_position(bbox)

ax1.plot(date,amount, color='gray', linewidth=0.3)
ax1.set_ylabel('2-4 Hz')

ax2.plot(date,amount, color='brown', linewidth=0.3)
ax2.set_ylabel('0.4-2 Hz')

ax3.bar (date, amount, color='gold', edgecolor='red', align='center')
ax3.set_ylabel('rainfall(mm/day)')

Related