I have the following code to make a Pie Chart with a Hole displaying the total value in the center:
import plotly.graph_objects as go
# Sample data
labels = ['Class A', 'Class B', 'Class C', 'Class D']
values = [10, 15, 5, 25]
# Create the Pie chart
fig = go.Figure(data=[go.Pie(labels=labels, values=values, hole=0.3)])
# Calculate the total
total = sum(fig.data[0].values)
# Add total text
total_annotation = fig.add_annotation(
text=f'{total}',
x=0.5,
y=0.5,
font=dict(size=20),
showarrow=False
)
# Set layout options
fig.update_layout(
title='Pie Chart with Total',
showlegend=True
).show()
The thing is that when I disable a given Class in the legend, the total value keeps displaying 55. Is there a way to update the total value to what is being displayed?