【问题标题】:Modify values and path in plotly express sunburst using updatemenus使用 updatemenus 修改 plotly express sunburst 中的值和路径
【发布时间】:2021-12-01 03:07:17
【问题描述】:

我正在使用 plotly express 绘制数据集作为旭日形图。 我想要实现的一件事是可以选择要绘制的值,以便在值发生变化时更新绘图,这意味着选择了数据框中的不同列。

我根据官方文档中的这个 sunburst 示例创建了一个示例 https://plotly.com/python/sunburst-charts/#sunburst-of-a-rectangular-dataframe-with-plotlyexpress

这里选择了“total_bill”列进行绘图并且有效。我可以在该示例中重新创建情节。 现在我想使用 updatemenus 将其切换到也包含浮点数并且应该可用的“提示”列。

我试过的示例代码:

import plotly.express as px

df = px.data.tips()

updatemenus = [{'buttons': [{'method': 'update',
                             'label': 'total_bill',
                             'args': [{'values': 'total_bill'}]
                              },
                            {'method': 'update',
                             'label': 'tip',
                             'args': [{'values': 'tip'}]
                             }],
                'direction': 'down',
                'showactive': True,}]

fig = px.sunburst(df, path=['day', 'time', 'sex'], values='total_bill')
fig.update_layout(updatemenus=updatemenus)
fig.show()

现在这将成功绘制与示例中相同的图,但是当我在两个更新菜单选项之间来回选择时,它的行为不正常。 我也尝试过在任何地方使用 Series,但结果是一样的。 我也看过这个例子,它有类似的重点 Plotly: How to select graph source using dropdown? 但是那里的答案也没有解决我的问题,因为旭日形在某种程度上似乎与散点图的行为不同?

知道如何让它工作吗?

【问题讨论】:

    标签: python plotly


    【解决方案1】:

    好的,我找到了一个可行的解决方案,但如果可能的话,也许有人可以为我指出一个“更清洁”的解决方案。 我偶然发现了这个实际上无关的问题: Plotly: How to create sunburst subplot using graph_objects?

    在那里,一种解决方案是保存图形表达图形的图形数据,并在图形对象图形中重用它。 这给了我一个想法,我可以保存每个图形的数​​据,然后在第三个图形中重用(和更新/修改)它。 而且,事实证明,这行得通!

    下面是工作示例:

    import plotly.express as px
    
    df = px.data.tips()
    
    # create two figures based on the same data, but with different values
    fig1 = px.sunburst(df, path=['day', 'time', 'sex'], values='total_bill')
    fig2 = px.sunburst(df, path=['day', 'time', 'sex'], values='tip')
    # save the data of each figure so we can reuse that later on
    ids1 = fig1['data'][0]['ids']
    labels1 = fig1['data'][0]['labels']
    parents1 = fig1['data'][0]['parents']
    values1 = fig1['data'][0]['values']
    ids2 = fig2['data'][0]['ids']
    labels2 = fig2['data'][0]['labels']
    parents2 = fig2['data'][0]['parents']
    values2 = fig2['data'][0]['values']
    
    # create updatemenu dict that changes the figure contents
    updatemenus = [{'buttons': [{'method': 'update',
                                 'label': 'total_bill',
                                 'args': [{
                                      'names': [labels1],
                                      'parents': [parents1],
                                      'ids': [ids1],
                                      'values': [values1]
                                     }]
                                  },
                                {'method': 'update',
                                 'label': 'tip',
                                 'args': [{
                                      'names': [labels2],
                                      'parents': [parents2],
                                      'ids': [ids2],
                                      'values': [values2]
                                     }]
                                 }],
                    'direction': 'down',
                    'showactive': True}]
    
    # create the actual figure to be shown and modified
    fig = px.sunburst(values=values1, parents=parents1, ids=ids1, names=labels1, branchvalues='total')
    fig.update_layout(updatemenus=updatemenus)
    fig.show()
    

    【讨论】:

      【解决方案2】:
      • 类似于您找到的解决方案。使用 Plotly Express 构建所有图形,收集到 dict
      • 现在可以使用dict理解构建菜单
      import plotly.express as px
      
      df = px.data.tips()
      
      # construct figures for all columns that can provide values
      figs = {
          c: px.sunburst(df, path=["day", "time", "sex"], values=c)
          for c in ["total_bill", "tip"]
      }
      
      # choose a column that becomes the figure
      fig = figs["total_bill"]
      # now build menus, that use parameters that have been pre-built using plotly express
      fig.update_layout(
          updatemenus=[
              {
                  "buttons": [
                      {
                          "label": c,
                          "method": "restyle",
                          "args": [
                              {
                                  "values": [figs[c].data[0].values],
                                  "hovertemplate": figs[c].data[0].hovertemplate,
                              }
                          ],
                      }
                      for c in figs.keys()
                  ]
              }
          ]
      )
      

      【讨论】:

      • 在这里使用字典是个好主意。谢谢你的建议。 :) 特别是对于 sunburst 情节,同时转移姓名、身份证和父母可能是个好主意。在您的示例中,它不应该有所作为,但如果应该通过下拉菜单更改路径,它会很有用。
      • 同意 - 应该是在 args 中使用更多参数的情况
      猜你喜欢
      • 2020-09-21
      • 2013-10-16
      • 2020-01-13
      • 1970-01-01
      • 1970-01-01
      • 2015-06-26
      • 1970-01-01
      • 2019-11-04
      • 2015-09-04
      相关资源
      最近更新 更多