简体   繁体   English

Plotly go 饼图:更改图例以显示另一列

[英]Plotly go Pie chart: change legend to show another column

I'm using plotly.graph_objects to make a pie chart of some data.我正在使用plotly.graph_objects制作一些数据的饼图。 (I'm having to use .go instead of plotly.express because I want the direction of the pie slices to display clockwise, something I couldn't find a way to control in .px .) (我不得不使用.go而不是plotly.express ,因为我希望饼图的方向显示为顺时针方向,而我无法找到在.px中控制的方法。)

The slices are calculated by the count column and labels are in Name , but I want the legend to show the information in the Access column.切片由count列计算,标签位于Name中,但我希望图例显示Access列中的信息。 Color coding means something in this case, and I want to display that information in the legend.在这种情况下,颜色编码意味着某些东西,我想在图例中显示该信息。

Is there a way to configure and change the column that the legend shows?有没有办法配置和更改图例显示的列? I've looked at the detailed documentation Plotly.go Pie objects and the Fundamentals: Legends with graph objects pages, but can't get it to work.我查看了详细的文档Plotly.go Pie objectsFundamentals: Legends with graph objects页面,但无法让它工作。 The only way I've found is to change the Pie.labels itself to Access , but that of course changes the shape of the pie slices.我发现的唯一方法是将Pie.labels本身更改为Access ,但这当然会改变饼图的形状。

MWE: MWE:

import pandas as pd
import plotly.graph_objects as go

test = {'Name': ['Company A','Company B','Company C','Company D','Company E'], 'count': [11,40,18,32,5], 'color':['red','blue','green','red','green'], 'Access':['Closed','Half','Free','Closed','Free']}
df = pd.DataFrame(data=test)
df

具有列名称、计数、颜色、访问权限的数据框

go_fig=go.Figure()
go_fig.add_trace(go.Pie(labels=df['Name'], values=df['count'], marker=dict(colors=df['color']), sort=False, direction='clockwise', textinfo='label+value+percent'))
go_fig

在此处输入图像描述

Add two traces, just like overlapping two layers.添加两条迹线,就像重叠两层一样。 The new trace would fall on the top but turn the new legend off so it can reveal the old legend underneath.新的痕迹会落在顶部,但会关闭新的图例,以便它可以显示下面的旧图例。

fig = go.Figure()
fig.add_trace(go.Pie(labels=df['Access'], values=df['count'], marker_colors=df['color']))
fig.add_trace(go.Pie(labels=df['Name'], values=df['count'], marker_colors=df['color'], showlegend=False))
fig.show()

在此处输入图像描述

It may be also useful to know that you can't change the legend, but you can customize the text displayed on the Pie chart by setting the text property and displaying it by using textinfo :知道您无法更改图例可能也很有用,但您可以通过设置text属性并使用textinfo显示它来自定义饼图上显示的文本:

Sets text elements associated with each sector.设置与每个扇区关联的文本元素。 If trace textinfo contains a “text” flag, these elements will be seen on the chart...如果跟踪文本信息包含“文本”标志,这些元素将在图表上看到...

go_fig=go.Figure()
go_fig.add_trace(go.Pie(labels=df['Access'], values=df['count'], text=df["Name"], textinfo='text+value+percent', marker=dict(colors=df['color']), sort=False, direction='clockwise'))

在此处输入图像描述

More in the docs 更多在文档中

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

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