简体   繁体   中英

Order of legend plotly

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)

目前情况如何

I would like to prespecify the order of the legend.

看起来现在唯一可用的控件是plotly 文档中layout.legend.traceorder属性。

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM