3

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.

Line plots last

Bar plots last

This is what I get so far (Edited by Trenton M):

  • Note the circled areas.

enter image description here

1
  • @ImportanceOfBeingErnest this question is more about the colors than the axes, if I'm reading this right Commented Sep 18, 2019 at 18:14

2 Answers 2

2

Figured it out! By leaving the dates as a column (i.e. 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()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

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:

enter image description here

6 Comments

I've messed around with the alpha a lot. Good thought but that's not working either. I've started going deep into the matplotlib parameters to try to figure this out because it seems like it's working for you and others. Any ideas on what could be off in the params?
I even tried uninstalling/reinstalling matplotlib just to go back to square 1 with no luck.
@JacobMiller The example output I am generating what you are looking for?
Your example is exactly what I am looking for, but I cannot for the life of me get it to plot like that on either my personal or work computer. Even starting from a brand new console, I create the simplified dataframe, copy and paste the code, and can't get both plots to show up. @ScottBoston
import pandas as pd import numpy as np import datetime import matplotlib.pyplot as plt 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]} date1 = pd.datetime(2018, 4, 10) date = [date1 + pd.Timedelta(days = i) for i in range(4)] temp_df = pd.DataFrame(data, index = date)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.