如何在3d散点图上自动标注



在这里,他们表明可以使用以下代码向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')]))

然而,当绘制时,它只显示最后一个注释,所以它重写注释,而不是每次迭代都写一个新的注释。有人知道如何自动化注释过程吗?在我的例子中,每个注释都有相同的文本,但是坐标不同。此外,我并没有对图上的每个点都做注释,只是做了一些注释。

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中有一个函数,并且通过重新分配它,您可以将其带走。

最新更新