【问题标题】:how to automate annotations on 3d scatter plotly plot如何在 3d 散点图上自动化注释
【发布时间】:2021-05-19 21:50:31
【问题描述】:

here,他们表明要向 3d 散点图添加注释,可以使用以下代码:

fig.update_layout(
    scene=dict(
        xaxis=dict(type="date"),
        yaxis=dict(type="category"),
        zaxis=dict(type="log"),
        annotations=[
        dict(
            showarrow=False,
            x="2017-01-01",
            y="A",
            z=0,
            text="Point 1",
            xanchor="left",
            xshift=10,
            opacity=0.7),
        dict(
            x="2017-02-10",
            y="B",
            z=4,
            text="Point 2",
            textangle=0,
            ax=0,
            ay=-75,
            font=dict(
                color="black",
                size=12
            ),
            arrowcolor="black",
            arrowsize=3,
            arrowwidth=1,
            arrowhead=1),
        dict(
            x="2017-03-20",
            y="C",
            z=5,
            ax=50,
            ay=0,
            text="Point 3",
            arrowhead=1,
            xanchor="left",
            yanchor="bottom"
        )]
    ),
)

这很好用,但是太手动了。我想自动化该过程,因为我有太多注释无法手动编写它们。

这是我的尝试:

for i in range(annotations):
    fig.update_layout(
        scene=dict(
            xaxis=dict(type='linear'),
            yaxis=dict(type='linear'),
            zaxis=dict(type='linear'),
            annotations=[
            dict(
                x=anx[i],
                y=any[i],
                z=anz[i],
                text='F')]))

但是,在绘制时,它只显示最后一个注释,因此它正在重写注释,而不是每次迭代都编写一个新注释。有谁知道如何自动化注释过程?就我而言,每个注释都有相同的文本,但坐标不同。另外,我没有为图上的每个点都添加注释,只是一些。

【问题讨论】:

    标签: python 3d annotations plotly


    【解决方案1】:

    fig.update_layout() 不像list.append 那样工作,每次调用它都会在现有集合中添加一些内容。它将根据提供的参数更新布局的配置,并且在循环中这样做只会显示您在上次迭代中设置的任何内容。

    annotations 参数接受一个字典列表,每个注释一个。你可以像这样自动化它

    ann = [dict(x=x, y=y, z=z, text='F') for x, y, z in zip(anx, any, anz)]
    fig.update_layout(
        scene=dict(
            xaxis=dict(type="date"),
            yaxis=dict(type="category"),
            zaxis=dict(type="log"),
            annotations=ann
        )
    )
    

    我还建议您为注释点的 y 坐标找到一个不同的名称,因为any 在 Python 中已经有一个函数,并且通过重新分配它可以将其去掉。

    【讨论】:

      猜你喜欢
      • 2012-05-09
      • 1970-01-01
      • 2016-08-20
      • 2012-02-22
      • 1970-01-01
      • 2018-06-19
      • 1970-01-01
      相关资源
      最近更新 更多