0

I'm trying to create a stacked clustured column line chart in python. However, I want to display the sum of only one column which is stacked. And I want to display the sum on top of that column.

Below is a short form of my code:

df = pd.read_excel('E:\Python Testing\InputDataPBI.xlsx',header=3)
#df.head(12)
df2 = df.drop(labels='Unnamed: 0',axis=1) 
df2['Timeperiod']= pd.DatetimeIndex(df2['Timeperiod']).month_name() 

plt.rcParams.update({'font.size': 14})
#plt.rcParams["figure.size"] = (20,3)

fig,ax = plt.subplots(dpi= 200)


ax2 = ax.twinx()

df2['Energ AV 2022%'] = df2['Energ AV 2022%'].multiply(100)
df2['Energ AV 2023%'] = df2['Energ AV 2023%'].multiply(100)

cn2 = df2.plot(x="Timeperiod",y=["Einspeisung (GWh)","Redispatch","Potenzial"],kind="bar", ax=ax, position=-0.5, color=["#669900","#FFFF00","#8EB3C0"],stacked=True, width= 0.25)


cn2.bar_label(cn2.containers[-1], fmt='%.2f')


cn = df2.plot(x="Timeperiod",y=["WiPla","Einspeisung [GWh] 2022"], kind="bar", ax=ax, position=0.75,color=["#C1B5A2","#C9C9C9"],width=0.5)




df2.plot(x="Timeperiod",y=["Energ AV 2022%"],ax=ax2, color='#B4B4B4')
df2.plot(x="Timeperiod",y=["Energ AV 2023%"],ax=ax2, color='#649BC0', linewidth=2, marker='o')


Its giving me the following output:

Output

In this output I wish to remove the 0.00 values as seen in x values May, June,........, December.

I tried to approach this problem by the ax.patches approach but still couldn't figure out.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
  • 3
    Full contained examples with data accesible should be provided for people to be able to respond to you :) – Carles May 05 '23 at 10:27
  • Something like `labels = [f'{h:0.2f}' if (h := v.get_height()) != 0 else '' for v in cn2.containers[-1]]` and `cn2.bar_label(cn2.containers[-1], labels=labels)`. However, can't test with an incomplete [mre] – Trenton McKinney May 05 '23 at 20:15

0 Answers0