0

I have a dataframe with a monthly datetime index and energy data: df.info() & df.head(). Each column is its own building on a campus. The goal is to plot a stacked bar of each month (and each building) on ax1 stacked barchart photo, and a cumulative lineplot on ax2 CuSum line attemped with stacked bars on the same chart. I have started on some code (pasted below). How can I combine the stacked bar plot of building kWh by month and cumulative kWh line plot into a single overlayed plot?

Code Below:

def stacked_bar_annual_cusum(frame, year):
    
    fig = plt.figure(figsize=(10, 7))
    ax_1 = fig.add_subplot(111)
    frame_raw = frame.copy()
    
    frame_annual = frame_raw.loc[frame_raw.index.year == year]
    
    frame_annual.plot(kind='bar', 
                    stacked=True, 
                    colormap='tab20', 
                    ax=ax_1)
    ax_1.set_xlabel("DateTime")
    ax_1.set_ylabel(f"Energy [kWh]")
    ax_1.legend(loc="upper left", ncol=1)
    ax_1.yaxis.set_major_formatter(mpl.ticker.StrMethodFormatter("{x:,.0f}"))
    plt.xlim(frame.index[0], frame.index[-1])
 
    # Create the second subplot to show the CuSum as a timeseries lineplot
    ax_2 = ax_1.twinx()
    frame_annual.sum(axis=1).cumsum().plot(kind='line', ax=ax_2, color="red", label="CuSum kWh")
    ax_2.set_ylabel(f"CuSum (Energy kWh)")
    ax_2.legend(loc=4)
    ax_2.yaxis.set_major_formatter(mpl.ticker.StrMethodFormatter("{x:,.0f}"))
    # plt.hlines(MEPC_kWh_pred, xmin=frame_annual.index[0], xmax=frame_annual.index[-1])
    plt.title(f"CY{year} Campus Energy Usage")
    fig.autofmt_xdate()
    plt.grid()
    plt.show()

stacked_bar_annual_cusum(frame=Joined_energy_data_trunc, year=2021)
2

0

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.