如何控制 Python Plotly 饼图的分段顺序
How do I control the order of the segments of a Python Plotly Pie chart
我在 Plotly.go 中绘制了一对简单的饼图。我已经按照我想要的顺序订购了值和标签(即标签 - 从低到高)。当我制作情节时,顺序被打乱了。我已经通读了 Plotly 文档和示例,但我无法找到如何修复排序。
作为第二个问题,如何消除 stick/spik?显示零标签。
import plotly.graph_objects as go
from plotly.subplots import make_subplots
labels = ["0","0.5", "1","1.5", "2","2.5", "3","3.5", "4","4.5", "5","5.5","6"]
# Create subplots: use 'domain' type for Pie subplot
fig = make_subplots(rows=1, cols=2, specs=[[{'type':'domain'}, {'type':'domain'}]])
fig.add_trace(go.Pie(labels=labels, values=[0,0,3,0,4,0,1,0,1,0,0,0,0], name="Wind Force",textinfo='label'),
1, 1)
fig.add_trace(go.Pie(labels=labels, values=[0,0,2,1,0,1,3,0,0,1,0,0,1], name="Wave Height",textinfo='label'),
1, 2)
# Use `hole` to create a donut-like pie chart
fig.update_traces(hole=.4, hoverinfo="label+percent+name")
fig.update_layout(
title_text="Wind and Wave conditions on site",
# Add annotations in the center of the donut pies.
annotations=[dict(text='Wind (Force)', x=0.17, y=0.5, font_size=20, showarrow=False),
dict(text='Wave (m)', x=0.82, y=0.5, font_size=20, showarrow=False)])
fig.show()
问题更新:
我现在已经编辑了标签和值以删除零值。这显然需要针对每个情节进行编辑,并不是一个通用的答案。
labelsW = ["1", "2", "3", "4"]
labelsS = ["1","1.5","2.5", "3","4.5","6"]
# Create subplots: use 'domain' type for Pie subplot
fig = make_subplots(rows=1, cols=2, specs=[[{'type':'domain'}, {'type':'domain'}]])
fig.add_trace(go.Pie(labels=labelsW, values=[3,4,1,1], name="Waver Height",textinfo='label', direction='clockwise', sort=False),
1, 1)
fig.add_trace(go.Pie(labels=labelsS, values=[2,1,1,3,1,1], name="Wind Force",textinfo='label', direction='clockwise', sort=False),1,2)
第一个问题我可以肯定回答,但第二个答案可能不太令人满意
1.) 要按照您提供的方式包含订单,请将这 2 个参数传递给 go.Pie() class( 方向 和 排序);
fig.add_trace(go.Pie(labels=labels, values=[0,0,3,0,4,0,1,0,1,0,0,0,0], name="Wind Force",textinfo='label', direction='clockwise', sort=False),
1, 1)
fig.add_trace(go.Pie(labels=labels, values=[0,0,2,1,0,1,3,0,0,1,0,0,1], name="Wave Height",textinfo='label', direction='clockwise', sort=False),
1, 2)
注意:您可能想设置 sort=True,看看它是否比 sort=False 更适合您。
2.) 将参数 textinfo 更改为 'none' 将删除所有标签。这就是为什么我注意到这可能不是一个完全令人满意的答案,因为它不仅会从零中删除标签,还会从所有其他条目中删除标签。
fig.add_trace(go.Pie(labels=labels, values=[0,0,3,0,4,0,1,0,1,0,0,0,0], name="Wind Force",textinfo='none', direction='clockwise', sort=False),
1, 1)
fig.add_trace(go.Pie(labels=labels, values=[0,0,2,1,0,1,3,0,0,1,0,0,1], name="Wave Height",textinfo='none', direction='clockwise', sort=False),
1, 2)
我希望至少第一部分对您有所帮助。
我在 Plotly.go 中绘制了一对简单的饼图。我已经按照我想要的顺序订购了值和标签(即标签 - 从低到高)。当我制作情节时,顺序被打乱了。我已经通读了 Plotly 文档和示例,但我无法找到如何修复排序。 作为第二个问题,如何消除 stick/spik?显示零标签。
import plotly.graph_objects as go
from plotly.subplots import make_subplots
labels = ["0","0.5", "1","1.5", "2","2.5", "3","3.5", "4","4.5", "5","5.5","6"]
# Create subplots: use 'domain' type for Pie subplot
fig = make_subplots(rows=1, cols=2, specs=[[{'type':'domain'}, {'type':'domain'}]])
fig.add_trace(go.Pie(labels=labels, values=[0,0,3,0,4,0,1,0,1,0,0,0,0], name="Wind Force",textinfo='label'),
1, 1)
fig.add_trace(go.Pie(labels=labels, values=[0,0,2,1,0,1,3,0,0,1,0,0,1], name="Wave Height",textinfo='label'),
1, 2)
# Use `hole` to create a donut-like pie chart
fig.update_traces(hole=.4, hoverinfo="label+percent+name")
fig.update_layout(
title_text="Wind and Wave conditions on site",
# Add annotations in the center of the donut pies.
annotations=[dict(text='Wind (Force)', x=0.17, y=0.5, font_size=20, showarrow=False),
dict(text='Wave (m)', x=0.82, y=0.5, font_size=20, showarrow=False)])
fig.show()
问题更新: 我现在已经编辑了标签和值以删除零值。这显然需要针对每个情节进行编辑,并不是一个通用的答案。
labelsW = ["1", "2", "3", "4"]
labelsS = ["1","1.5","2.5", "3","4.5","6"]
# Create subplots: use 'domain' type for Pie subplot
fig = make_subplots(rows=1, cols=2, specs=[[{'type':'domain'}, {'type':'domain'}]])
fig.add_trace(go.Pie(labels=labelsW, values=[3,4,1,1], name="Waver Height",textinfo='label', direction='clockwise', sort=False),
1, 1)
fig.add_trace(go.Pie(labels=labelsS, values=[2,1,1,3,1,1], name="Wind Force",textinfo='label', direction='clockwise', sort=False),1,2)
第一个问题我可以肯定回答,但第二个答案可能不太令人满意
1.) 要按照您提供的方式包含订单,请将这 2 个参数传递给 go.Pie() class( 方向 和 排序);
fig.add_trace(go.Pie(labels=labels, values=[0,0,3,0,4,0,1,0,1,0,0,0,0], name="Wind Force",textinfo='label', direction='clockwise', sort=False),
1, 1)
fig.add_trace(go.Pie(labels=labels, values=[0,0,2,1,0,1,3,0,0,1,0,0,1], name="Wave Height",textinfo='label', direction='clockwise', sort=False),
1, 2)
注意:您可能想设置 sort=True,看看它是否比 sort=False 更适合您。
2.) 将参数 textinfo 更改为 'none' 将删除所有标签。这就是为什么我注意到这可能不是一个完全令人满意的答案,因为它不仅会从零中删除标签,还会从所有其他条目中删除标签。
fig.add_trace(go.Pie(labels=labels, values=[0,0,3,0,4,0,1,0,1,0,0,0,0], name="Wind Force",textinfo='none', direction='clockwise', sort=False),
1, 1)
fig.add_trace(go.Pie(labels=labels, values=[0,0,2,1,0,1,3,0,0,1,0,0,1], name="Wave Height",textinfo='none', direction='clockwise', sort=False),
1, 2)
我希望至少第一部分对您有所帮助。