【发布时间】:2020-10-17 03:37:06
【问题描述】:
简述
我认为我的问题有两个部分:
-
plotly期望ids参数采用什么格式? - 如何创建
ids以允许我创建带有重复标签的树形图?
更详细的
我正在尝试创建一个带有共享标签的嵌套树形图。例如,将其视为“其他”类别。
这里我使用的是 Python 3.8 Plotly 4.9.0
重新利用plotly 示例:
from plotly import graph_objects as go
labels = ["A1", "A2", "A3", "A4", "A5", "B1", "B2", "Other", "Other"]
parents = ["", "A1", "A2", "A3", "A4", "", "B1", "A1", "B1"]
ids = [f"{parents[i]}{'-' if parents[i] else ''}{labels[i]}" for i in range(0,len(labels))]
# ['A1', 'A1-A2', 'A2-A3', 'A3-A4', 'A4-A5', 'B1', 'B1-B2', 'A1-Other', 'B1-Other']
fig = go.Figure(go.Treemap(
labels = labels,
parents = parents,
ids=ids # commenting this out displays something at least
))
fig.show()
上面给了我一个空白屏幕,但是如果我注释掉 ids 参数,它会忽略其中一个重复的标签:
我查看了plotly 文档的各个部分,看起来这个字符串列表应该可以按预期工作。
从https://plotly.com/python/reference/treemap/中提取:
ID
类型:列表、numpy 数组或 Pandas 系列数字、字符串或 日期时间。为每个数据分配 id 标签。这些用于对象恒定性的 id 动画期间的数据点。应该是一个字符串数组,而不是 数字或任何其他类型。
我尝试ids 的这种特定结构的原因是由于其他plotly 示例
例如https://plotly.com/python/treemaps/#controlling-text-fontsize-with-uniformtext
import plotly.graph_objects as go
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/718417069ead87650b90472464c7565dc8c2cb1c/sunburst-coffee-flavors-complete.csv')
fig = go.Figure(go.Treemap(
ids = df.ids,
labels = df.labels,
parents = df.parents))
fig.update_layout(uniformtext=dict(minsize=10, mode='hide'))
fig.show()
# Above is exact example, below is df structure
df.head()
ids labels parents
0 Aromas Aromas NaN
1 Tastes Tastes NaN
2 Aromas-Enzymatic Enzymatic Aromas
3 Aromas-Sugar Browning Sugar Browning Aromas
4 Aromas-Dry Distillation Dry Distillation Aromas
感谢您的宝贵时间!
【问题讨论】:
标签: python plotly plotly-python