[英]Plotly Pie Chart and label order
如何更改饼图中标签的顺序(绘图)?
我想强制执行此命令: 20 16 15
而不是16 15 20
我的 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
我的蟒蛇代码
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()
有两件事:
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)
请参阅此Repl.it以获取有效的演示(它会生成带有绘图的html页面)。
图例顺序将与标签中的顺序相对应(除非图表中的sort = True
True
(默认情况下为True
))。 您要做的是按照降序对“ 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()
使用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".
对于在 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.