5

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.

jlcope
  • 34
  • 6
Michieldo
  • 169
  • 1
  • 2
  • 15

3 Answers3

5

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

Massifox
  • 4,369
  • 11
  • 31
2

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.

Alex
  • 188
  • 11
1

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.

nicolaskruchten
  • 26,384
  • 8
  • 83
  • 101