I have two pandas series. I need to plot them with shared X axis stacked on each other. I tried the code below but instead of stacking them it rather puts the two lines on the same plot. Why is that? How can I stack them?
n=10
x = pd.Series(data = np.random.randint(0,10, n), index=pd.date_range(pd.to_datetime('today'), freq='D', periods=n))
y = pd.Series(data = np.random.randint(0,10, n), index=pd.date_range(pd.to_datetime('today'), freq='D', periods=n))
ax = x.plot()
ax2 = y.plot()
ax2.sharex(ax)
I know the stacking can be done with subplots. If I understood correctly, pandas' only handle is the ax
that is returned upon calling df.plot()
. How can I use this ax
to achieve the proper stacking with shared abscissa?
CodePudding user response:
Use subplots
to create shared axis:
fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True)
x.plot(ax=ax1)
y.plot(ax=ax2)
plt.show()