简体   繁体   中英

Plotting line plot on top of bar plot in Python / matplotlib from dataframe

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.

在此处输入图片说明

Figured it out! By leaving the dates as a column (ie not setting them as the index), I can plot both the line plot and bar plot. I can then go back and adjust labels accordingly.

@ScottBoston your x-axis tipped me off. Thanks for looking into this.

date1 = pd.datetime(2018, 4, 10)
data = {'LightlyActive': [314, 253, 282, 292],
    'FairlyActive': [34, 22, 26, 35],
    'VeryActive': [123, 102, 85, 29],
    'efficiency': [93.0, 96.0, 93.0, 96.0],
    'wake': [55.0, 44.0, 47.0, 43.0],
    'light': [225.0, 260.0, 230.0, 205.0],
    'deep': [72.0, 50.0, 60.0, 81.0],
    'rem': [99.0, 72.0, 97.0, 85.0],
    'date': [date1 + pd.Timedelta(days = i) for i in range(4)]}
temp_df = pd.DataFrame(data)

fig, ax1 = plt.subplots(figsize = (10, 10))
ax2 = plt.twinx(ax = ax1)
temp_df[['LightlyActive', 'FairlyActive', 'VeryActive']].\
         plot(kind = 'bar', stacked = True, ax = ax1)
temp_df[['wake', 'light', 'deep', 'rem']].plot(ax = ax1, alpha = 0.5)
temp_df['efficiency'].plot(ax = ax2)
ax1.set_xticklabels(labels = temp_df['date'])
plt.show()

在此处输入图片说明

What about using alpha ?

fig, ax1 = plt.subplots(figsize = (10, 10))
temp_df[['LightlyActive', 'FairlyActive', 'VeryActive']].plot(kind = 'bar', stacked = True, ax = ax1, alpha=.3)
ax2 = plt.twinx(ax = ax1)
temp_df[['wake', 'light', 'deep', 'rem']].plot(ax = ax1, zorder=10)
temp_df['efficiency'].plot(ax = ax2)
plt.show()

Output:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM