简体   繁体   English

如何使用场景向 scatter3D Plotly plot 添加注释?

[英]How to add annotations to a scatter3D Plotly plot using scene?

I am trying to add a text annotation to points in a scatter3d Plotly plot with a different scene.我正在尝试向具有不同场景的 scatter3d Plotly plot 中的点添加文本注释。 How can I make the annotation move around with the plot?如何使注释随 plot 移动? Even when I sign xref and yref as 'scene' the annotation doesn't move.即使我将外部参照和 yref 签署为“场景”,注释也不会移动。

This is a reproducible example I am trying to run:这是我正在尝试运行的可重现示例:

library(plotly)

rep.ex = data.frame(x1=1:20, y1=1:20, z1=(1:20)*2, text1=letters[1:20])

axoff <- list(title = "", zeroline = FALSE, showline = FALSE, showticklabels = FALSE, showgrid = FALSE, autotick = F)
axoffxy <- list(title = "", zeroline = FALSE, showline = FALSE, showticklabels = FALSE, showgrid = FALSE, autotick = F, showspikes=F)


plot_ly(data=data.frame(rep.ex), x=rep.ex$x1, y=rep.ex$y1, z=rep.ex$z1, 
            marker=list(size=2.6), 
            color=rep.ex$x1, hoverinfo='text',
            text=rep.ex$text1,
            type="scatter3d", mode = "markers") %>%
  layout(showlegend = T, dragmode="turntable", scene = list(aspectmode='cube', xaxis = axoffxy, yaxis = axoffxy, zaxis = axoff), 
         annotations=list(showarrow=FALSE, 
                          text='Here I insert my annotation',  
                          xref='scene',     
                          yref='scene',
                          zref='scene',
                          x=1,  
                          y=1, 
                          z=2,
                          xanchor='left',   
                          yanchor='bottom',  
                          font=list(size=12 )))

I am using Plotly version 4.9.0我正在使用 Plotly 版本 4.9.0

If your problem allows you to switch from annotations in layout to add_trace it will move around.如果您的问题允许您从布局中的注释切换到add_trace它会移动。 Something like this will work for that:像这样的东西将为此工作:

plot_ly(data=data.frame(rep.ex), x=rep.ex$x1, y=rep.ex$y1, z=rep.ex$z1, 
        marker=list(size=2.6), color=rep.ex$x1, hoverinfo='text',
        text=rep.ex$text1, type="scatter3d", mode = "markers", showlegend=F) %>%
  add_trace(type='scatter3d', mode='text', x=10,y=10,z=10, 
            text='Here I insert my annotation', showlegend=F, inherit=F) %>%
  layout(showlegend = T, dragmode="turntable", 
         scene = list(aspectmode='cube', xaxis = axoffxy, yaxis = axoffxy, zaxis = axoff))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM