Chi
Chi

Reputation: 197

share X axis and change size in the same time with subplots in python

I have a dataframe as show below

df =
       index         value   active  
2014-05-21 10:00:00    0.0        1        
2014-05-21 11:00:00    3.4        1        
2014-05-21 12:00:00    nan        0      
2014-05-21 13:00:00    0.0        1        
2014-05-21 14:00:00    nan        0        
2014-05-21 15:00:00    1.0        1 
.....       

and plot it by subplots

the code and the figure are showed below

f, (ax11, ax12)= plt.subplots(2, 1, sharex=True)

ax11.plot(df.index, df.value, c='g')
ax12.scatter(df.index, df.active, s = 5)
ax12.xaxis([-2, -1, 0, 1, 2])
plt.tight_layout()
ax11.grid(True), ax12.grid(True)
plt.show()

enter image description here

I would like to make figure ax11 two times bigger than ax12 and share the xaxis in the same time.

I have checked the tutorial of subplot2grid, gridspec, they both can give differents subplot size, however it seems only subplots could share the x- or y-axis, how can I do them both in the same time? Anyone have an idea? Thanks in advance !

Upvotes: 1

Views: 1643

Answers (1)

Ami Tavory
Ami Tavory

Reputation: 76316

You just need to set gridspec_kw in your subplots call; otherwise, there's nothing to change.

gridspec_kw : dict, optional Dict with keywords passed to the GridSpec constructor used to create the grid the subplots are placed on.

Here's a simple example:

fig, (ax1, ax2) = subplots(2, sharex=True, gridspec_kw={'height_ratios': [2, 1]})
ax1.plot([1, 2, 3]);
ax2.plot([3, 2, 4]);

enter image description here

Upvotes: 1

Related Questions