如何从Plotly Sunburst图表中检索值?

发布于 2025-01-23 02:42:05 字数 621 浏览 4 评论 0原文

Plotly Sunburst Charts 非常适合可视化层次数据。是否可以将图表中显示的值检索到字典或数组之类的内容中?

具体地说,假设以下数据框:

Att1   Att2
A      C
A      D
B      D
B      D
B      C

px.sunburst(数据,['att1','att2'])将生成一个图表,该图表在最内部的环中具有a a <的值2 /code>和3用于b。然后,对于a,它将指示有1 c和1 d。同样,对于b,它将指示2 d和1 c。所有这些数字都是我想要检索的数字。情节有这样的功能吗?还是我最好的选择是data.groupby迭代?

Plotly sunburst charts are great for visualizing hierarchical data. Is it possible to retrieve the values shown in the chart into a dictionary, or an array or something?

Concretely, assume the following dataframe:

Att1   Att2
A      C
A      D
B      D
B      D
B      C

px.sunburst(data, ['Att1', 'Att2']) will generate a chart that in the most inner ring has the value 2 for A and 3 for B. Then for A, it will indicate there is 1 C and 1 D. Similarly, for B it will indicate 2 D and 1 C. All those numbers are the ones I am looking to retrieve. Does plotly have such functionality? or is my best bet to use data.groupby iteratively?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小镇女孩 2025-01-30 02:42:05

可以访问来自图的基础数据,并且可以找到详细信息在Plotly 和总结下面...

查看任何任何的数据结构
plotly.graph_objects.figure对象,包括plitly返回的对象
可以通过打印(无花果)或在jupyterlab中完成
特殊图show(“ json”)渲染器。图还支持图。
和图to_json()方法。打印()图将导致
经常用verbose布局。图板键表示为椭圆形'...'
为了简洁。

有几个选项可用...

  • 图。Show(“ JSON”)如果您在笔记本
  • print(Fig)是我的方法

中,则是我的hany。此绘图示例:

import plotly.express as px
data = dict(
    character=["Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"],
    parent=["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve" ],
    value=[10, 14, 12, 10, 2, 6, 6, 4, 4])

fig = px.sunburst(
    data,
    names='character',
    parents='parent',
    # values='value',
)
fig.show()

print(fig)将返回:

Figure({
    'data': [{'domain': {'x': [0.0, 1.0], 'y': [0.0, 1.0]},
              'hovertemplate': 'character=%{label}<br>parent=%{parent}<extra></extra>',
              'labels': array(['Eve', 'Cain', 'Seth', 'Enos', 'Noam', 'Abel', 'Awan', 'Enoch', 'Azura'],
                              dtype=object),
              'name': '',
              'parents': array(['', 'Eve', 'Eve', 'Seth', 'Seth', 'Eve', 'Eve', 'Awan', 'Eve'],
                               dtype=object),
              'type': 'sunburst'}],
    'layout': {'legend': {'tracegroupgap': 0}, 'margin': {'t': 60}, 'template': '...'}
})

然后有了一些元组知识,您可以提取一些信息,例如...

print(图.data [0] .Labels)。将返回:

['Eve' 'Cain' 'Seth' 'Enos' 'Noam' 'Abel' 'Awan' 'Enoch' 'Azura']

The underlying data from a figure can be accessed and the details can be found here at plotly and summarized below...

Viewing the underlying data structure for any
plotly.graph_objects.Figure object, including those returned by Plotly
Express, can be done via print(fig) or, in JupyterLab, with the
special fig.show("json") renderer. Figures also support fig.to_dict()
and fig.to_json() methods. print()ing the figure will result in the
often-verbose layout.template key being represented as ellipses '...'
for brevity.

Several options are available...

  • fig.show("json") is pretty hany if you're in a notebook
  • print(fig) is my go to method

So for this plotly example:

import plotly.express as px
data = dict(
    character=["Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"],
    parent=["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve" ],
    value=[10, 14, 12, 10, 2, 6, 6, 4, 4])

fig = px.sunburst(
    data,
    names='character',
    parents='parent',
    # values='value',
)
fig.show()

print(fig) will return:

Figure({
    'data': [{'domain': {'x': [0.0, 1.0], 'y': [0.0, 1.0]},
              'hovertemplate': 'character=%{label}<br>parent=%{parent}<extra></extra>',
              'labels': array(['Eve', 'Cain', 'Seth', 'Enos', 'Noam', 'Abel', 'Awan', 'Enoch', 'Azura'],
                              dtype=object),
              'name': '',
              'parents': array(['', 'Eve', 'Eve', 'Seth', 'Seth', 'Eve', 'Eve', 'Awan', 'Eve'],
                               dtype=object),
              'type': 'sunburst'}],
    'layout': {'legend': {'tracegroupgap': 0}, 'margin': {'t': 60}, 'template': '...'}
})

And then with some knowledge of tuples you can extract some info, for example...

print(fig.data[0].labels) will return:

['Eve' 'Cain' 'Seth' 'Enos' 'Noam' 'Abel' 'Awan' 'Enoch' 'Azura']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文