简体   繁体   English

Plotly:如何生成并排的 px.sunburst 图?

[英]Plotly: How to generate side-by-side px.sunburst plots?

I am using the following Python code to produce a sunburst plot:-我正在使用以下 Python 代码来生成森伯斯特图:-

import plotly
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
df = pd.read_csv('CellUsageStats.csv',dtype={'Population':int},keep_default_na=False)
print(df.head(50))
fig = px.sunburst(df, path=['LibPrefix', 'MasterPrefixAbrHead', 'MasterPrefixAbrTail', 'Drive'], values='Population', color='Population', color_continuous_scale=px.colors.sequential.Inferno)
fig.update_traces(hovertemplate='Pattern: %{currentPath}%{label}<br>Matches: %{value}<br>Percent of <b>%{parent}</b>: %{percentParent:0.2%f}<br>Percent of <b>%{entry}</b>: %{percentEntry:0.2%f}<br>Percent of <b>%{root}</b>: %{percentRoot:0.2%f}')
fig.show()
plotly.offline.plot(fig, filename='sunburst.html')

I have a second csv file that I use to produce another sunburst, using the same code above.我有第二个 csv 文件,用于生成另一个森伯斯特,使用上面相同的代码。 How do I combine them to create side-by-side plots in the same html output?如何组合它们以在同一 html 输出中创建并排图? I found...我发现...

https://plotly.com/python/sunburst-charts/ https://plotly.com/python/sunburst-charts/

However, I really need px.sunburst attributes, and the examples in the above link use go.Sunburst, not px.sunburst.但是,我确实需要 px.sunburst 属性,而上面链接中的示例使用的是 go.Sunburst,而不是 px.sunburst。 Is there a straightforward way to produce side-by-side plots from two different sets of data?有没有一种直接的方法可以从两组不同的数据中生成并排图?

Thanks!谢谢!

Here's a modified version of @vestland's answer that makes use of subplots:这是使用子图的@vestland 答案的修改版本:

import plotly.express as px
from plotly.subplots import make_subplots

# data for px.sunburst1
data1 = 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])

# data for px.sunburst2
data2 = dict(character=["Eve", "Cain", "Seth", "Enos"],
             parent=["", "Eve", "Eve", "Seth"],
             value=[10, 14, 12, 10])

# extract data and structure FROM px.sunburst1
sb1 = px.sunburst(data1,
                  names='character',
                  parents='parent',
                  values='value',
                  )

# extract data and structure FROM px.sunburst2
sb2 = px.sunburst(data2,
                  names='character',
                  parents='parent',
                  values='value',
                  )

fig = make_subplots(rows=1, cols=2, specs=[
    [{"type": "sunburst"}, {"type": "sunburst"}]
])

fig.add_trace(sb1.data[0], row=1, col=1)
fig.add_trace(sb2.data[0], row=1, col=2)
fig.show()

Answer回答

The following suggestion will show you how you can:以下建议将向您展示如何:

1. build two separate px.Sunburst figures, and 1.构建两个单独的px.Sunburst图形,以及

2. extract all necessary info regarding data and structure from them using ._data , and 2.使用._data从中提取有关数据和结构的所有必要信息,以及

3. use that to build two go.Sunburst traces, and 3.使用它来构建两个go.Sunburst痕迹,以及

4. set them up side by side using the domain attribute. 4.使用domain属性并排设置它们。

In other words, you form your figures using px.Sunburst , and steal what you need from there to easily set up two sunburst diagrams side by side.换句话说,您可以使用px.Sunburst形成您的图形,并从那里窃取您需要的东西,以便轻松地并排设置两个森伯斯特图。

Plot阴谋

在此处输入图片说明

Complete code完整代码

import plotly
import plotly.offline as py
import plotly.graph_objs as go
import plotly.express as px

# data for px.sunburst1
data1 = 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])

# data for px.sunburst2
data2 = dict(character=["Eve", "Cain", "Seth", "Enos"],
             parent=["", "Eve", "Eve", "Seth" ],
             value=[10, 14, 12, 10])

# extract data and structure FROM px.sunburst1
sb1 =px.sunburst(data1,
                 names='character',
                 parents='parent',
                 values='value',
                )._data

# extract data and structure FROM px.sunburst2
sb2 =px.sunburst(data2,
                 names='character',
                 parents='parent',
                 values='value',
                )._data

# traces with separate domains to form a subplot
trace1 = go.Sunburst(labels=sb1[0]['labels'],
                        parents=sb1[0]['parents'],
                        values=sb1[0]['values'],
                        domain={'x': [0.0, 0.5], 'y': [0.0, 1]})

trace2 = go.Sunburst(labels=sb2[0]['labels'],
                        parents=sb2[0]['parents'],
                        values=sb2[0]['values'],
                        domain={'x': [0.5, 1.0], 'y': [0., 1.00]})

# layout and figure production
layout = go.Layout(height = 600,
                   width = 600,
                   autosize = False,
                   title = 'Side by side px.Sunburst diagrams')
fig = go.Figure(data = [trace1, trace2], layout = layout)
fig.show()

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

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