Piechart force to not order by pie size

Hello, I’m trying to compare two different dataset with different group size. Here is a quick example

import plotly.graph_objects as go
from plotly.subplots import make_subplots
df = pd.DataFrame([["Group 1", 67], ["Group 2", 81], ["Group 3", 40], ["Group 4", 79]], columns=["Population", "Size"])
df2 = pd.DataFrame([["Group 1", 87], ["Group 2", 45], ["Group 3", 105], ["Group 4", 46]], columns=["Population", "Size"])

colorList = ["orange", "red", "yellow", "maroon"]

fig = make_subplots(rows=1, cols=2, specs=[[{"type": "domain"}, {"type": "domain"}]])

fig.add_trace(go.Pie(values=df["Size"], labels=df["Population"], textinfo='label+percent',
                     marker_colors=colorList), 1, 1)
fig.add_trace(go.Pie(values=df2["Size"], labels=df2["Population"], textinfo='label+percent',
                     marker_colors=colorList), 1, 2)

It’s difficult to compare the different groups because their positions are changing. Is it possible to fix their position and not have anti-clockwise pie based on sample

Just add this line at the end:

fig.update_traces(sort=False, selector=dict(type='pie'))

1 Like