here , they show that to add annotations to a 3d scatter plot one can use the following code:
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"
)]
),
)
That works fine, but it's too manual. I'd like to automate that process because I have too many annotations to write them manually.
This is my attempt:
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')]))
However, when plotted, it only shows the last annotation, so it's rewriting the annotations, instead of writing a new one every iteration. Does anyone know how to automate the annotation process? In my case, every annotation has the same text, but the coordinates are different. Also, I am not including annotations for every point on the plot, just some.
fig.update_layout()
does not work like list.append
, where every time it's called it adds something to the already existing collection. It will update the configuration of the layout based on the arguments provided and doing so in a loop will only show you whatever you set it to in the last iteration.
The annotations
arguments takes a list of dicts, one for each annotation. You can automate it like this
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
)
)
I also suggest you find a different name for the y coordinates of the annotated points, because any
already has a function in Python and by reassigning it you take that away.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.