I want to plot two charts (top-bottom) in one figure, using matplotlib.pyplot and dataframe.plot, respectively. But the below code generates two figures, with an empty bottom plot in the first figure. I am not sure how to merge them into one figure and would highly appreciate some help.
fig, [ax1, ax2] = plt.subplots(2,1,sharex = True)
ax1.plot(info['Yr'], info['mean'], label = 'mean')
ax1.plot(info['Yr'], info['CI-2.5%'], label = 'CI-2.5%')
ax1.plot(info['Yr'], info['CI-97.5%'], label = 'CI-97.5%')
ax2 = df.set_index('Yr').plot.bar(stacked=True)
CodePudding user response:
You can supply the ax
argument to the plot.bar
command with the ax you created and it'll use it, effectively "merging" your two plots.
fig, [ax1, ax2] = plt.subplots(2,1,sharex = True)
ax1.plot(info['Yr'], info['mean'], label = 'mean')
ax1.plot(info['Yr'], info['CI-2.5%'], label = 'CI-2.5%')
ax1.plot(info['Yr'], info['CI-97.5%'], label = 'CI-97.5%')
ax2 = df.set_index('Yr').plot.bar(stacked=True, ax = ax2)
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.html
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.bar.html