WillH
WillH

Reputation: 303

How do I control the order of the segments of a Python Plotly Pie chart

I have a simple pair of Pie charts plotting in Plotly.go. I have ordered the Values and Labels in the order I want (i.e. Labels - Low to high). When I produce the plot, the order is scrambled. I have read through the Plotly documentation and examples but I can't find out how to fix the ordering. As a second question, how do I eliminate the stick/spik? showing the Zero labels.

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

labels = ["0","0.5", "1","1.5", "2","2.5", "3","3.5", "4","4.5", "5","5.5","6"]

# Create subplots: use 'domain' type for Pie subplot
fig = make_subplots(rows=1, cols=2, specs=[[{'type':'domain'}, {'type':'domain'}]])

fig.add_trace(go.Pie(labels=labels, values=[0,0,3,0,4,0,1,0,1,0,0,0,0], name="Wind Force",textinfo='label'),
              1, 1)
fig.add_trace(go.Pie(labels=labels, values=[0,0,2,1,0,1,3,0,0,1,0,0,1], name="Wave Height",textinfo='label'),
              1, 2)

# Use `hole` to create a donut-like pie chart
fig.update_traces(hole=.4, hoverinfo="label+percent+name")

fig.update_layout(
    title_text="Wind and Wave conditions on site",
    # Add annotations in the center of the donut pies.
    annotations=[dict(text='Wind (Force)', x=0.17, y=0.5, font_size=20, showarrow=False),
                 dict(text='Wave (m)', x=0.82, y=0.5, font_size=20, showarrow=False)])
fig.show()

Question Update: I have now edited the Labels and Values to remove the Zero values. This obviously needs to be edited for every plot and isn't a universal answer.

labelsW = ["1", "2", "3", "4"]
labelsS = ["1","1.5","2.5", "3","4.5","6"]

# Create subplots: use 'domain' type for Pie subplot
fig = make_subplots(rows=1, cols=2, specs=[[{'type':'domain'}, {'type':'domain'}]])

fig.add_trace(go.Pie(labels=labelsW, values=[3,4,1,1], name="Waver Height",textinfo='label', direction='clockwise', sort=False),
          1, 1)
fig.add_trace(go.Pie(labels=labelsS, values=[2,1,1,3,1,1], name="Wind Force",textinfo='label', direction='clockwise', sort=False),1,2)

Upvotes: 1

Views: 2141

Answers (1)

waykiki
waykiki

Reputation: 1094

I can answer the first question for sure, but the second answer might not be satisfactory.

1.) To have the order contained the way you provided it with, pass these 2 arguments to the go.Pie() class (direction and sort);

fig.add_trace(go.Pie(labels=labels, values=[0,0,3,0,4,0,1,0,1,0,0,0,0], name="Wind Force",textinfo='label', direction='clockwise', sort=False),
          1, 1)
fig.add_trace(go.Pie(labels=labels, values=[0,0,2,1,0,1,3,0,0,1,0,0,1], name="Wave Height",textinfo='label', direction='clockwise', sort=False),
              1, 2)

Note: you may want to set sort=True, and see whether it suits you better than sort=False.

2.) Changing the parameter textinfo to 'none' will remove all labels. This is why I noted that this may not be a fully satisfactory answer, as it will remove not only the labels from the zeros, but from all other entries as well.

fig.add_trace(go.Pie(labels=labels, values=[0,0,3,0,4,0,1,0,1,0,0,0,0], name="Wind Force",textinfo='none', direction='clockwise', sort=False),
          1, 1)
fig.add_trace(go.Pie(labels=labels, values=[0,0,2,1,0,1,3,0,0,1,0,0,1], name="Wave Height",textinfo='none', direction='clockwise', sort=False),
              1, 2)

I hope that at least the first part is helpful.

Upvotes: 2

Related Questions