Search code examples
pythondataframetime-seriesspydersubplot

How to share the x-axis in these stack plots


I am trying to share the have the same x-axis for these two plots, but the sharex=true is not working as I thought it would.

fig, ax = plt.subplots(figsize=(12,5),sharex=True)
ax = plt.subplot(211)
sns.lineplot(x ='Date', y = 'Air1', data = df, label = 'Temp °C') 
ax = plt.subplot(212)
sns.lineplot(x ='Date', y = 'Air2', data = df, label = 'Temp °C')

Thanks in advance!

enter image description here


Solution

  • Can you try this code?

    fig=plt.figure(figsize=(12,5))
    ax1 = plt.subplot(211)
    sns.lineplot(x ='Date', y = 'Air1', data = df, label = 'Temp °C') 
    ax2 = plt.subplot(212)
    sns.lineplot(x ='Date', y = 'Air2', data = df, label = 'Temp °C') 
    ax1.get_shared_x_axes().join(ax1, ax2)
    ax1.set_xticklabels([])
    

    I don't know your data but i tried it with my own created data and i got this output, please give feedback if it's wrong. enter image description here