有没有办法改变日出图中某一层的厚度? [英] Is there a way to vary the thickness of a layer in sunburst diagram in plotly

查看:25
本文介绍了有没有办法改变日出图中某一层的厚度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改日出图中某一层的厚度。我已经查看了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()

现在看起来是这样的:

这是我想要的部门:

这可能吗?

推荐答案

据我所知,这并不容易实现。user documentationGithub documentation中没有任何内容表明存在允许您修改每一层的比例的参数。不过,再深入一点也无伤大雅。

如果我们查看以下代码示例:

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'
) 

如果我们运行这段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'}]

它有点凌乱,但据我所知,日出图表或多或少是使用Ploly中的各种其他图表构建的,包括Barpolarpiegraph_Objects。

在图user documentation中,Barpoly图_对象如下所示:

您可以指定每个钢筋的半径,但默认情况下,它们都将从原点向外辐射。但是,如果你真的想要创建不均匀层厚的太阳爆发图,并且你愿意努力做到这一点,你可以尝试自己在条形图的顶部构建它:我的建议是将彩色条形图覆盖在白色条形图上,并为每个条形图测试不同的半径。以下是您可以实际实现的一个示例:

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()

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

注意:任何比我更有Ploly经验的人,如果我在这一点上是错误的,如果每一层的厚度确实可以通过SunBurst图形_对象中的参数来控制,请纠正我。

这篇关于有没有办法改变日出图中某一层的厚度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆