我想在图例下放置一个下拉菜单。但是,根据分辨率的大小,plotly更改此下拉列表的位置(参见上传的图片)。
第一个图显示了输出,就像 Rstudio 小绘图选项卡中显示的那样。第二个显示如果我切换到全屏,下拉菜单会向右移动多远。
如何固定下拉菜单的位置?任何解决方案都值得赞赏,无论是html,R还是其他任何解决方案
您可以在下面找到我用来创建绘图的代码:
library(plotly)
x <- seq(-2 * pi, 2 * pi, length.out = 1000)
df <- data.frame(x, y1 = sin(x), y2 = cos(x),tan_h=tanh(x))
p <- plot_ly(df, x = ~x) %>%
add_lines(y = ~y1, name = "sin") %>%
add_lines(y = ~y2, name = "cos") %>%
add_lines(y = ~tan_h, name='tanh',visible=FALSE) %>%
layout(
title = "Drop down menus - Styling",
xaxis = list(domain = c(0.1, 1)),
yaxis = list(title = "y"),
updatemenus = list(
list(
y = 0.7,
x = 1.1,
buttons = list(
list(method = "restyle",
args = list("visible", list(TRUE, TRUE, FALSE)),
label = "Cos"),
list(method = "restyle",
args = list("visible", list(TRUE, FALSE, TRUE)),
label = "Tanh"),
list(method = "restyle",
args = list("visible", list(TRUE, TRUE, TRUE)),
label = "All")
))
)
)
Run Code Online (Sandbox Code Playgroud)
小智 2
这个问题现在已经很老了,但是对于Python,你现在至少可以通过plotly API定位这个问题,如下所示:https: //plotly.com/python/v3/dropdowns/#style-dropdown
您可以将参数x、y、xanchor和传递yanchor给updatemenusplotly 方法的graphobjects Figure参数update_layout:
import plotly.graph_objects as go
fig = go.Figure()
fig.update_layout(
title="Demo",
updatemenus=[go.layout.Updatemenu(
active=0,
buttons=[{"label": "Demo button...", "args": [{"showlegend": False}]}],
x = 0.3,
xanchor = 'left',
y = 1.2,
yanchor = 'top',
)
]
)
fig.show()
Run Code Online (Sandbox Code Playgroud)