【问题标题】:Dynamically Changing X-axis and Y-axis Titles Using layout.updatemenus[] Dropdown使用 layout.updatemenus[] Dropdown 动态改变 X 轴和 Y 轴标题
【发布时间】:2020-08-06 14:38:38
【问题描述】:

我有 Python Dash 应用程序,它显示带有多个跟踪的 Plotly graph_object.scatter plot,我可以使用 layout.updatemenus[] 下拉菜单在它们之间切换。

我面临的问题是,根据选择的下拉菜单选项,x-axisy-axis 应该有不同的标题。

到目前为止,两个轴都没有任何标题,但我可以通过使用 xaxis_titleyaxis_title figure.update_layout 方法。但是,我不能传递 listxaxis_titleyaxis_title 的字典(它只接受字符串或 int)。

我目前正在设想一系列 if/else 语句,用于查找当前将“ShowActive”设置为 True 的下拉菜单选项,并据此更改 xaxis_title 和 yaxis_title 属性。但我不知道如何编写,以便在选择新菜单选项时运行此轴标题更改功能。

如有必要,我可以使用代码示例更新此问题。

【问题讨论】:

  • 附带说明,我知道如果我使用 dcc.dropdown 并设置回调而不是使用 layout.updatemenus[],则有一种方法可以做到这一点,但由于我拥有应用程序当前设置,这将需要大量工作并影响应用程序的许多其他元素来进行更改,所以我想尽可能避免这种情况。
  • 请添加mcve。这个answer 可以帮忙吗?

标签: python drop-down-menu plotly axis-labels


【解决方案1】:

您可以在更新时更改 x 轴标签和 y 轴标签,如下所示:

updatemenus = list([
dict(active=-1,
     buttons=list([   
        dict(label = 'High',
             method = 'update',
             args = [{'visible': [True, True, False, False]},
                     {'title': 'Yahoo High',
                      'xaxis': {'title': 'Date'},
                      'yaxis': {'title': 'High Price - Dollars'},
                      'annotations': high_annotations}]),
        dict(label = 'Low',
             method = 'update',
             args = [{'visible': [False, False, True, True]},
                     {'title': 'Yahoo Low',
                      'xaxis': {'title': 'Date'},
                      'yaxis': {'title': 'Low Price - Dollars'},
                      'annotations': low_annotations}]),

args 中,您可以更改'xaxis' : {'title' : 'CHANGE X-AXIS LABEL'} 来实现此目的。

答案来自:https://community.plotly.com/t/changing-the-xy-axis-titles-with-dropdown-menu/5421/2.

【讨论】:

    猜你喜欢
    • 2023-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多