Text and Annotation on Subplots when one plot is PieChart

Hi Plotly Community.
I have subplots whose one of plot is PieChart. I tried multiple ways to place an annotation.
Here is my code which is not working.

import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots

fig_l = make_subplots(
    rows=1, cols=2,
    specs=[[{"type": "domain"},{"type": "xy"}]], print_grid=True)


fig_l.add_trace(go.Pie(labels=grouped_df_3['department'], values=grouped_df_3['counting'],
                hole = .4),
            row=1, col=1)


fig_l.add_trace(go.Bar(
    x=group_salary['department'],
    y=group_salary['placed_perc'],
    name='Employed',
    text = group_salary['placed_perc']),
    row=1, col=2)
fig_l.add_trace(go.Bar(
    x=group_salary['department'],
    y=group_salary['not_placed_perc'],
    name="Unemployed",
    text=group_salary['not_placed_perc']
), row = 1, col = 2)

fig_l.add_annotation(xref='x domain',
                   yref='y domain',
                   x=0.01,
                   y=0.9,
                   text='Pie Chart', 
                   showarrow=False,
                   row=1, col=1)


fig_l.add_annotation(xref='x domain',
                   yref='y domain',
                   x=0.01,
                   y=0.9,
                   text='Bar Chart', 
                   showarrow=False,
                   row=1, col=2)


This is giving an error of

ValueError: Cannot add annotation to subplot at position (1, 1) because subplot is of type domain.

Then I tried

fig_l.update_layout(annotations=[dict(text='GHG', x=0.18, y=0.5, font_size=20, showarrow=False),
                 dict(text='CO2', x=0.82, y=0.5, font_size=20, showarrow=False)])

This also not assigning any text to Pie Chart, instead assigning both texts to Bar Chart.


Would be grateful if anyone can help, thank you very much!

@Usmanjon
When one of the subplots is a pie chart, then the annotations are set with respect to paper:

import plotly.graph_objects as go
import plotly.express as px
from plotly.subplots import make_subplots


# This dataframe has 244 lines, but 4 distinct values for `day`
df = px.data.tips()
pie = px.pie(df, values='tip', names='day')
fig=make_subplots(rows=1, cols=2,
                  specs=[[{"type": "domain"},{"type": "xy"}]])

fig.add_trace(pie.data[0], row=1, col=1)
fig.add_trace(go.Bar(x=['A', 'B', 'C'], y=[25, 17, 19], marker_color="blue", name= "Quantity"), row=1, col=2)
fig.update_layout(width=700, height=350, bargap=0.05)

fig.add_annotation(dict(x=0.22, y=-0.1,   ax=0, ay=0,
                    xref = "paper", yref = "paper", 
                    text= "Hello, pie chart!"
                  ))
fig.add_annotation(dict(x=0.72, y=-0.14,   ax=0, ay=0,
                    xref = "paper", yref = "paper", 
                    text= "My Bars!"
                  ))

1 Like

Thank you very much @empet , it solved my problem. Is this something written or just annotations don’t work if one is pie subplot, and the other is not Pie subplot? Because when both of subplots are Pie, It is just working without setting it to paper.

@Usmanjon When one subplot is a pie chart (referenced to a polar system) , and another a chart, referenced to a cartesian system of coordinates it cannot find/place within the pie chart cell a position given in cartesian coordinates.

1 Like

Okay now I understand, many thanks!

@empet Hi, Sorry one more question. Setting xref and yref to paper are producing another problem. When changed from part screen to full screen, the position of text is changing as paper does not take into account the distance of relative plots. Do you have any idea how to solve that? Thanks again!!

@Usmanjon Instead of annotations try to use text, and textinfo with text inserted outside of pie, via textposition="outside".
Inspect what is displayed when calling:
help(go.Pie.text), and help(go.Pie.textinfo) to be able to add text near a sector, instead of an annotation.

1 Like