How to create a stacked area chart using hvplot or holoviews in python?

189 Views Asked by At

I am trying to replicate the graph below using hvplot or holoviews. Plot I want to replicate

This is the data that I am working with.

Data I am working with

The data contains a 'Cluster' column that consist of 11 clusters, each cluster has 24 hours to it. Each hour has an associated power output, corresponding to a specific technology. Using this data I would like to replicate the graph above.

This is the hvplot.area code I wrote.

dfCP_CCS.hvplot.area(x='Hr', y='Power', groupby=['Cluster', 'Tech'], 
stacked=True)

Result of above code

The closest I could get was using a bar plot.

This is the hvplot.bar code I wrote.

dfCP_CCS.hvplot(kind='bar', x='Cluster', y='Power', by='Tech')

Bar Plot

Which is not what I am looking for.

Any suggestions?

1

There are 1 best solutions below

0
Rico On

I was able to generate something closer to what I desired, however, it was not using hvplot or holoviews. The plot was generated using the pandas.plot() methods, although I felt I should share my solution.

It was how the data was structured within the pandas data frame, I needed to pivot the data frame on the Cluster and Hours column, using the power as the values, and creating the columns with the tech column. This allowed me to access the power output for each technology on a cluster and hourly basis. See the code below for the pivoting.

dfCP_CCS = dfPower_CCS.pivot_table(values='Power',
                         index=['Cluster', 'Hr'],
                         columns=['Tech'],
                         aggfunc=np.sum,
                         fill_value=0)

The result: New Dataframe after pivot

Using the new df I wrote the code below:

# With CCS
fig, ax = plt.subplots()

ax2 = ax.twiny()

dfCP_CCS.plot(
    ax = ax,
    kind='area',
        title='Reliability Metrics for Techonology with CCS',
        xlabel='',
        ylabel='Power Output(TWh)',
        colormap=cmap)

dfCD.plot(ax = ax2, kind='line', linestyle='dashdot', color='red', xlabel='')

# remove upper axis ticklabels
ax2.set_xticklabels([])

ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05), ncol=5)
ax2.legend(['Demand'],loc='upper right', bbox_to_anchor=(0.3, 1))


plt.savefig(imgPath+"\\WithCCUSReliability.png", bbox_inches='tight')

Result: Stacked Area Chart