4
votes

Just like a similar question asked here, I would like to change the order of the legend of a plotly pie chart. The difference is that I can't simply use traceorder since I have more than 6 labels and reversing them doesn't put them in the right order.

Is there a way to customize the order?

As an example with 4 labels:

import plotly.offline as py
trace1 = go.Pie(                    
    values = [2, 4, 4, 9],                    
    labels = ['A', 'B', 'C', 'D'],
    name    = "Succes" 
)

data = [trace1]
fig  = go.Figure(data = data)
py.plot(fig)

How it's currently

I would like to prespecify the order of the legend.

3

3 Answers

3
votes

It looks like the only control that is available right now is the layout.legend.traceorder attribute in the plotly docs

2
votes

Seems like your best bet is to either reorder the input/column to the way that you want it prior to plotting it, as Plotly will then just reference that order.

OR

Try and use traceorder (specifically Grouped) as @Massifox mentions.

0
votes

For pie charts, the legend and the slice order are linked, and by default the slices are sorted. To disable sorting, you can set sort=False inside go.Pie(), which will cause your slices and the corresponding legend to appear in the order you specify in your data arrays.