有没有办法改变旭日图中图层的厚度

Tim*_*imo 6 python plotly sunburst-diagram

我想更改旭日图中图层的厚度。我已经浏览了https://plotly.com/python/sunburst-charts/上的所有示例,但找不到任何好的解决方案。

采取下面的例子(来自上面的链接)

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()
Run Code Online (Sandbox Code Playgroud)

这是现在的样子: 就像现在一样

这就是我想要划分的地方: 我想在哪里划分

这可能吗?

Der*_*k O 4

据我所知,这并不容易实现。用户文档Github 文档中没有任何内容表明有一个参数允许您修改每层的比例。不过,再深入一点也没什么坏处。

如果我们看下面的代码示例:

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'
) 
Run Code Online (Sandbox Code Playgroud)

如果我们运行此 Python 代码并输入fig.layout.template.data,我们可以看到用于生成旭日图的图表类型和相应参数:

layout.template.Data({
    'bar': [{'error_x': {'color': '#2a3f5f'},
             'error_y': {'color': '#2a3f5f'},
             'marker': {'line': {'color': '#E5ECF6', 'width': 0.5}},
             'type': 'bar'}],
    'barpolar': [{'marker': {'colorbar': {'len': 0.9, 'lenmode': 'fraction'}, 'line': {'color': '#E5ECF6', 'width': 0.5}},
                  'type': 'barpolar'}],
    'carpet': [{'aaxis': {'endlinecolor': '#2a3f5f',
                          'gridcolor': 'white',
                          'linecolor': 'white',
                          'minorgridcolor': 'white',
                          'startlinecolor': '#2a3f5f'},
                'baxis': {'endlinecolor': '#2a3f5f',
                          'gridcolor': 'white',
                          'linecolor': 'white',
                          'minorgridcolor': 'white',
                          'startlinecolor': '#2a3f5f'},
                'type': 'carpet'}],
    'choropleth': [{'colorbar': {'outlinewidth': 0, 'ticks': ''}, 'type': 'choropleth'}],
    'contour': [{'colorbar': {'outlinewidth': 0, 'ticks': ''},
                 'colorscale': [[0.0, '#0d0887'], [0.1111111111111111, '#46039f'],
                                [0.2222222222222222, '#7201a8'],
                                [0.3333333333333333, '#9c179e'],
                                [0.4444444444444444, '#bd3786'],
                                [0.5555555555555556, '#d8576b'],
                                [0.6666666666666666, '#ed7953'],
                                [0.7777777777777778, '#fb9f3a'],
                                [0.8888888888888888, '#fdca26'], [1.0, '#f0f921']],
                 'type': 'contour'}],
    'contourcarpet': [{'colorbar': {'outlinewidth': 0, 'ticks': ''}, 'type': 'contourcarpet'}],
    'heatmap': [{'colorbar': {'outlinewidth': 0, 'ticks': ''},
                 'colorscale': [[0.0, '#0d0887'], [0.1111111111111111, '#46039f'],
                                [0.2222222222222222, '#7201a8'],
                                [0.3333333333333333, '#9c179e'],
                                [0.4444444444444444, '#bd3786'],
                                [0.5555555555555556, '#d8576b'],
                                [0.6666666666666666, '#ed7953'],
                                [0.7777777777777778, '#fb9f3a'],
                                [0.8888888888888888, '#fdca26'], [1.0, '#f0f921']],
                 'type': 'heatmap'}],
    'heatmapgl': [{'colorbar': {'outlinewidth': 0, 'ticks': ''},
                   'colorscale': [[0.0, '#0d0887'], [0.1111111111111111,
                                  '#46039f'], [0.2222222222222222, '#7201a8'],
                                  [0.3333333333333333, '#9c179e'],
                                  [0.4444444444444444, '#bd3786'],
                                  [0.5555555555555556, '#d8576b'],
                                  [0.6666666666666666, '#ed7953'],
                                  [0.7777777777777778, '#fb9f3a'],
                                  [0.8888888888888888, '#fdca26'], [1.0,
                                  '#f0f921']],
                   'type': 'heatmapgl'}],
    'histogram': [{'marker': {'colorbar': {'outlinewidth': 0, 'ticks': ''}}, 'type': 'histogram'}],
    'histogram2d': [{'colorbar': {'outlinewidth': 0, 'ticks': ''},
                     'colorscale': [[0.0, '#0d0887'], [0.1111111111111111,
                                    '#46039f'], [0.2222222222222222, '#7201a8'],
                                    [0.3333333333333333, '#9c179e'],
                                    [0.4444444444444444, '#bd3786'],
                                    [0.5555555555555556, '#d8576b'],
                                    [0.6666666666666666, '#ed7953'],
                                    [0.7777777777777778, '#fb9f3a'],
                                    [0.8888888888888888, '#fdca26'], [1.0,
                                    '#f0f921']],
                     'type': 'histogram2d'}],
    'histogram2dcontour': [{'colorbar': {'outlinewidth': 0, 'ticks': ''},
                            'colorscale': [[0.0, '#0d0887'], [0.1111111111111111,
                                           '#46039f'], [0.2222222222222222,
                                           '#7201a8'], [0.3333333333333333,
                                           '#9c179e'], [0.4444444444444444,
                                           '#bd3786'], [0.5555555555555556,
                                           '#d8576b'], [0.6666666666666666,
                                           '#ed7953'], [0.7777777777777778,
                                           '#fb9f3a'], [0.8888888888888888,
                                           '#fdca26'], [1.0, '#f0f921']],
                            'type': 'histogram2dcontour'}],
    'mesh3d': [{'colorbar': {'outlinewidth': 0, 'ticks': ''}, 'type': 'mesh3d'}],
    'parcoords': [{'line': {'colorbar': {'outlinewidth': 0, 'ticks': ''}}, 'type': 'parcoords'}],
    'pie': [{'automargin': True, 'type': 'pie'}],
    'scatter': [{'marker': {'colorbar': {'outlinewidth': 0, 'ticks': ''}}, 'type': 'scatter'}],
    'scatter3d': [{'line': {'colorbar': {'outlinewidth': 0, 'ticks': ''}},
                   'marker': {'colorbar': {'outlinewidth': 0, 'ticks': ''}},
                   'type': 'scatter3d'}],
    'scattercarpet': [{'marker': {'colorbar': {'outlinewidth': 0, 'ticks': ''}}, 'type': 'scattercarpet'}],
    'scattergeo': [{'marker': {'colorbar': {'outlinewidth': 0, 'ticks': ''}}, 'type': 'scattergeo'}],
    'scattergl': [{'marker': {'colorbar': {'outlinewidth': 0, 'ticks': ''}}, 'type': 'scattergl'}],
    'scattermapbox': [{'marker': {'colorbar': {'outlinewidth': 0, 'ticks': ''}}, 'type': 'scattermapbox'}],
    'scatterpolar': [{'marker': {'colorbar': {'outlinewidth': 0, 'ticks': ''}}, 'type': 'scatterpolar'}],
    'scatterpolargl': [{'marker': {'colorbar': {'outlinewidth': 0, 'ticks': ''}}, 'type': 'scatterpolargl'}],
    'scatterternary': [{'marker': {'colorbar': {'outlinewidth': 0, 'ticks': ''}}, 'type': 'scatterternary'}],
    'surface': [{'colorbar': {'outlinewidth': 0, 'ticks': ''},
                 'colorscale': [[0.0, '#0d0887'], [0.1111111111111111, '#46039f'],
                                [0.2222222222222222, '#7201a8'],
                                [0.3333333333333333, '#9c179e'],
                                [0.4444444444444444, '#bd3786'],
                                [0.5555555555555556, '#d8576b'],
                                [0.6666666666666666, '#ed7953'],
                                [0.7777777777777778, '#fb9f3a'],
                                [0.8888888888888888, '#fdca26'], [1.0, '#f0f921']],
                 'type': 'surface'}],
    'table': [{'cells': {'fill': {'color': '#EBF0F8'}, 'line': {'color': 'white'}},
               'header': {'fill': {'color': '#C8D4E3'}, 'line': {'color': 'white'}},
               'type': 'table'}]
Run Code Online (Sandbox Code Playgroud)

这有点混乱,但据我所知,旭日图或多或少是使用 Plotly 中的各种其他图表(包括 graph_objects)构建Barpolarpie

从 Plotly用户文档来看,Barpolar graph_object 看起来像这样:

在此输入图像描述

您可以指定每个条形的半径,但默认情况下它们都会从原点向外辐射。但是,如果您确实想创建层厚度不均匀的旭日图,并且您愿意努力做到这一点,您可以尝试自己在条形图之上构建它:我的建议是将彩色条覆盖在白色上条并测试每个条的不同半径。以下是您可以实际实现的目标的示例:

import plotly.graph_objects as go

fig = go.Figure()

## create bars of larger radius beneath
fig.add_trace(go.Barpolar(
    r=[5, 5],
    theta=[67.5, 180],
    width=[135, 90],
    marker_color=["salmon","yellow"],
    marker_line_color="white",
    marker_line_width=2,
    opacity=1
    )
)

fig.add_trace(go.Barpolar(
    r=[2, 2],
    theta=[67.5, 180],
    width=[135, 90],
    marker_color=["blue","green"],
    marker_line_color="white",
    marker_line_width=2,
    opacity=1
    )
)

## overlay a bar that removes the central circle
fig.add_trace(go.Barpolar(
    r=[1],
    theta=[0],
    width=[360],
    marker_color=["white"],
    marker_line_color="white",
    marker_line_width=2,
    opacity=1
    )
)

fig.update_layout(
    template=None,
    polar = dict(
        radialaxis = dict(range=[0, 6], showticklabels=False, ticks=''),
        angularaxis = dict(showticklabels=False, ticks='')
    )
)

fig.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

然后,您需要为悬停模板中的名称和值添加注释。您基本上是从头开始构建图表,所以我承认这将是一个艰苦的过程。

注意:任何比我有更多 Plotly 经验的人,如果我在这一点上错了,并且每层的厚度确实可以通过 Sunburst graph_object 中的参数控制,请纠正我。