I am trying to plot a line plot on top of a stacked bar plot in matplotlib, but cannot get them both to show up.
I have the combined dataframe already set up by pulling various information from other dataframes and with the datetime index. I am trying to plot a stacked bar plot from the activity columns (LightlyActive, FairlyActive, VeryActive) and several line plots from the minutes in each sleep cycle (wake, light, deep, rem) on one set of axes (ax1). I am then trying to plot the efficiency column as a line plot on a separate set of axes (ax2).
I cannot get both the stacked bar plot and the line plots to show up simultaneously. If I plot the bar plot second, that is the only one that shows up. If I plot the line plots first (activity and efficiency) those are the only ones that show up. It seems like whichever style of plot I plot second covers up the first one.
LightlyActive FairlyActive VeryActive efficiency wake light deep rem
dateTime
2018-04-10 314 34 123 93.0 55.0 225.0 72.0 99.0
2018-04-11 253 22 102 96.0 44.0 260.0 50.0 72.0
2018-04-12 282 26 85 93.0 47.0 230.0 60.0 97.0
2018-04-13 292 35 29 96.0 43.0 205.0 81.0 85.0
fig, ax1 = plt.subplots(figsize = (10, 10))
temp_df[['LightlyActive', 'FairlyActive', 'VeryActive']].plot(kind = 'bar', stacked = True, ax = ax1)
ax2 = plt.twinx(ax = ax1)
temp_df[['wake', 'light', 'deep', 'rem']].plot(ax = ax1)
temp_df['efficiency'].plot(ax = ax2)
plt.show()
I would like to have on single plot with a stacked bar plot of activity levels ('LightlyActive', 'FairlyActive', 'VeryActive') and sleep cycles ('wake', 'light', 'deep', 'rem') on one set of axes, and sleep efficiency on a second set of axes.
EDIT
I am not even getting it to display as Trenton did in the edited version below (designated as "Edited by Trenton M"). The 2 plots immediately below this are the versions that display for me.
This is what I get so far (Edited by Trenton M):
- Note the circled areas.




