简体   繁体   English

Plotly 饼图和标签顺序

[英]Plotly Pie Chart and label order

How to change the order of the labels in the Pie Chart (plotly)?如何更改饼图中标签的顺序(绘图)?

I want to force this order: 20 16 15我想强制执行此命令: 20 16 15

And not 16 15 20而不是16 15 20


My csv file:我的 csv 文件:

id,A,B,C
1,15,16,45
2,20,15,54
3,16,18,60
4,16,15,54
5,15,12,68
6,16,20,68

My python code我的蟒蛇代码

import pandas
import plotly.graph_objects as go

col_label = "A"
col_values = "Count"

data = pandas.read_csv(mycsvfile)
v = data[col_label].value_counts()
new = pandas.DataFrame({
    col_label: v.index,
    col_values: v.values
})
fig = go.Figure(
    data=[go.Pie(
        labels=new[col_label],
        values=new[col_values])
    ])
fig.show()

Gives this graph:给出这张图: 图形

There's 2 things: 有两件事:

import pandas
import plotly.graph_objects as go

col_label = "A"
col_values = "Count"

data = pandas.read_csv("mycsvfile")
v = data[col_label].value_counts()
new = pandas.DataFrame({
    col_label: v.index,
    col_values: v.values
})
# First, make sure that the data is in the order you want it to be prior to plotting 
new = new.sort_values(
  by=col_label, 
  ascending=False)

fig = go.Figure(
    data=[go.Pie(
        labels=new[col_label],
        values=new[col_values],
        # Second, make sure that Plotly won't reorder your data while plotting
        sort=False)
    ])
fig.write_html('first_figure.html', auto_open=False)

See this Repl.it for a working demo (it produces the html page with the plot). 请参阅此Repl.it以获取有效的演示(它会生成带有绘图的html页面)。

The legend order will be corresponding to order in labels (unless the sort = True in a chart which is True by default). 图例顺序将与标签中的顺序相对应(除非图表中的sort = True True (默认情况下为True ))。 What you have to do is to order the 'A' values in descending order and then to create a plot with adding parameter sort=False 您要做的是按照降序对“ A”值进行排序,然后创建带有添加参数sort=False

import pandas
import plotly.graph_objects as go

col_label = "A"
col_values = "B"

data = pandas.read_csv(mycsvfile)
v = data[col_label].value_counts()
new = pandas.DataFrame({
    col_label: v.index,
    col_values: v.values
})
new = new.sort_values('A', ascending=False)

fig = go.Figure(
    data=[go.Pie(
        labels=new[col_label],
        values=new[col_values],
        sort=False
        )
    ])
fig.show()

Use the layout.legend.traceorder attribute such as: 使用layout.legend.traceorder属性,例如:

traceorder (flaglist string) 

Any combination of "reversed", "grouped" joined with a "+" OR "normal". 
examples: "reversed", "grouped", "reversed+grouped", "normal" 
Determines the order at which the legend items are displayed. 

If "normal", the items are displayed top-to-bottom in the same order 
as the input data. If "reversed", the items are displayed in the opposite order 
as "normal". If "grouped", the items are displayed in groups (when a trace
`legendgroup` is provided).  If "grouped+reversed", the items are displayed in the 
opposite order as "grouped".

See more in the official documentation . 请参阅官方文档中的更多内容

For people who have the same question in React (javascript), add sort: false to the data props of your Plot component:对于在 React (javascript) 中有相同问题的人,请将sort: false添加到 Plot 组件的data属性中:

const data = [{
    values: myValues,
    labels: myLabels,
    sort: false
}];

return (
    <Plot data={data} />
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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