Is there a way to create a go.pie which automatically picks out the top 5 or 10 values

I have a column called Brand and another called Model, I would like to show the top 5 or 10 brands automatically without manually entering based on the total number of models per brand.

My code is;

labels = [ 'Audi','Nissan', 'Tesla','Volkswagen','Skoda']
values = [9,8,13,8,6]
fig = go.Figure(data=[go.Pie(labels=labels,
                             values=values,
                             title="Top 5 Manufacturers with Most Vehicles",
                             titleposition='top left',
                             pull=[0, 0, 0.2, 0])])

fig.update_layout(
    font=dict(
       family="Times New Roman",
       size=20,
       color='black',
                              ))

fig.update_traces(textfont_size=18)

fig.update_layout(height=535,
                  width=1150,
                  margin=dict(t=0, b=0, l=0, r=0),
                  font=dict(size=18),
                  title=dict(
                    y=1,
                    x=0.5,
                  xanchor= 'left',
                  yanchor= 'top'),
                  legend=dict(
                  orientation="h",
                  yanchor="bottom",
                  y=0.01,
                  xanchor="right",
                  x=0.95))

fig.show()

Many thanks

Hi @Dashz !

It’s more about data preparation, try something like:

labels = ['Audi', 'Nissan', 'Tesla', 'Volkswagen', 'Skoda']
values = [9, 8, 13, 8, 6]
n_top = 2

top_values, top_labels = zip(*sorted(zip(values, labels))[-n_top:])

If there are tie values where the data is split, you probably need something a little fancier.

1 Like

@Dashz this is more of a Python rather Plotly/Dash question. I’m sure if you look it up on Stackoverflow people have already answered your question.

1 Like

Thanks for your reply as always, at present unfortunately I have a tight deadline to use this code and have tried Stackoverflow . I’ve very much appreciated this forum where individuals such as yourself have been greatly helpful.

Many thanks