【问题标题】:Is there a way to change the opacity of the leafs in a plotly.express.sunburst figure?有没有办法改变 plotly.express.sunburst 图中叶子的不透明度?
【发布时间】:2021-10-16 15:28:04
【问题描述】:

我不喜欢具有不同不透明度的叶子类别,我找不到更改它的设置。

我找到了this related topic,但我使用的是 plotly.express 而不是 plotly.graph_object。有没有办法改变它,还是我必须尝试将我的 px-figure 更改为 graph_object? example image

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

【问题讨论】:

    标签: python plotly sunburst-diagram plotly-express


    【解决方案1】:

    无论你是使用Plotly Express 还是Plotly Graph Objects 来构建你的身材,这都有效:

    fig.update_traces(leaf=dict(opacity = 1))
    

    完整代码:

    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.update_traces(leaf=dict(opacity = 1))
    
    fig.show()
    

    【讨论】:

      【解决方案2】:

      正如相关主题中提到的,透明度可以通过相应的图形对象来控制。

      import plotly.graph_objects as go
      
      fig =go.Figure(go.Sunburst(
          labels=data['character'],
          parents=data['parent'],
          values=[10, 14, 12, 10, 2, 6, 6, 4, 4],
          leaf=dict(opacity=1),
      ))
      
      fig.update_layout(margin = dict(t=0, l=0, r=0, b=0))
      
      fig.show()
      

      【讨论】:

      • 我认为他们改变透明度是有充分理由的。我认为原因是为了澄清亲子关系。如果我的回答对你有帮助,请考虑采纳为正确答案
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-30
      • 1970-01-01
      • 2020-09-27
      • 2019-07-07
      • 2011-03-12
      • 2020-11-07
      相关资源
      最近更新 更多