How to add multi-line annotation at the center of a donut chart?

It seems to me that I can only add one line of annotation at the center of a donut chart using a f-string for the “text” property of the .add_annotation function of a donut chart.

What if I want to add multi-line text for this annotation, e.g., showing “Total Value” above the numerical value?

I don’t know if I understand your purpose correctly, but if you want to add a multi-line annotation in the center of a doughnut graph, you can use string merging. I have slightly modified the data for the donut graph example in the reference.

import plotly.graph_objects as go

labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen']
values = [45.00, 25.00, 10.53, 5.00]

# Use `hole` to create a donut-like pie chart
fig = go.Figure(data=[go.Pie(labels=labels, values=values, hole=.3)])

fig.add_annotation(
    x=0.5,
    y=0.5,
    text='total values:<br>'+f'{round(sum(values),2):,}',
    font=dict(size=16, weight='bold'),
    showarrow=False,
)
fig.update_layout(height=600)
fig.show()

1 Like

Thanks, @r-beginners

Yes, using <br> to break the line is the solution that I’m looking for. Now I know how to add multiline text annotation.

Much obliged.