【问题标题】:Plotly: Change marker text in scatter plot on on_click evetPlotly:在 onclick 事件上更改散点图中的标记文本
【发布时间】:2020-09-16 15:42:50
【问题描述】:

我正在关注如何使用点击事件更新散点图中的点的示例 https://plotly.com/python/click-events/.

示例中的回调函数在单击时更新标记的颜色和大小。 (代码提取后见我的问题)

# create our callback function
def update_point(trace, points, selector):
    id=trace.ids[points.point_inds[0]]
    c = list(scatter.marker.color)
    s = list(scatter.marker.size)
    for i in points.point_inds:
        #change only this one
        c[i] = '#bae2be'
        s[i] = 20
        with f.batch_update():
            scatter.marker.color = c
            scatter.marker.size = s

我的问题是:如何在同一次点击中更新标记文本(在本例中为 Scatter 对象的文本参数)?

谢谢

迈克尔

【问题讨论】:

    标签: python callback plotly


    【解决方案1】:

    经过一番挖掘:

    文本不是标记对象的属性。相反,它是 Scatter 对象的列表类型属性,这意味着您必须更新文本列表的适用元素。

    以下是原示例之后更新后的回调函数:

    # create our callback function
    def update_point(trace, points, selector):
        id=trace.ids[points.point_inds[0]]
        c = list(scatter.marker.color)
        s = list(scatter.marker.size)
        for i in points.point_inds:
            #change only this one
            c[i] = '#bae2be'
            s[i] = 20
            **ids[i]+=ids[i] #duplicate text to test effect**
            with f.batch_update():
                scatter.marker.color = c
                scatter.marker.size = s
                **f.data[0]['text']=ids #update the entire text-list-property**
    

    【讨论】:

      猜你喜欢
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 2016-07-29
      • 1970-01-01
      • 1970-01-01
      • 2020-05-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多