Example post: Plotting Total Annual Cost contribution (Pie Chart)

Hi Everyone

Here is a simple example of how to plot the contributions to the total annual costs from a model run, presented as a pie chart. The contributions are split into resource, investment, and maintenance costs.
This visualization is useful to get a quick overview on what contributes how much to the total system costs.
Feel free to suggest any improvements!

Resulting plot:

Here is the code to create it:

import plotly.express as px
import numpy as np
from energyscope.energyscope import Energyscope
from energyscope.models import core
import pandas as pd


# Create a model and calculate the results
es_core = Energyscope(model=core)
results = es_core.calc()


C_inv = results.variables["C_inv"].copy()
C_maint = results.variables["C_maint"].copy()
C_op = results.variables["C_op"].copy()
tau = results.parameters["tau"].copy()

# annualize investment costs
C_inv["C_in"] = C_inv["C_in"] * tau["tau"].values

techs = C_inv.index
resources = C_op.index

# Create a sunburst chart using Plotly
# with inner ring: C_inv, C_maint, C_op and outer ring: technologies and resources
# Define root and cost categories
# Fill the DataFrame with the relevant data
# Inner ring
df_inner = pd.DataFrame({
    'cost_type': ['Investment', 'Maintenance', 'Resource'],
    'tech_resource': [''] * 3,
    'cost': [sum(C_inv.values[:,0]), sum(C_maint.values[:,0]), sum(C_op.values[:,0])]
})
# Outer ring
df_outer = pd.DataFrame({
    'cost_type': (['Investment'] * len(techs)) + (['Maintenance'] * len(techs)) + (['Resource'] * len(resources)),
    'tech_resource': list(techs) + list(techs) + list(resources),
    'cost': list(C_inv.values[:,0]) + list(C_maint.values[:,0]) + list(C_op.values[:,0])
})
df_data = pd.concat([df_inner, df_outer], ignore_index=True)
# drop the first 3 rows with empty tech_resource
df_data = df_data.drop(index=[0, 1, 2])


# Create a sunburst chart using Plotly
fig = px.sunburst(df_data, path=['cost_type', 'tech_resource'], values='cost',
                         color='cost', color_continuous_scale='YlOrRd')

fig.update_layout(title_text='Total Annualized Cost', title_x=0.5, width=600, height=600)
fig.show(renderer="notebook")