简体   繁体   English

Plotly:并排地块之间的 shareX

[英]Plotly: shareX between side-by-side plots

I would like to have two side by side plots sharing the same X-axis and the same toolbar.我想要两个并排的图共享相同的 X 轴和相同的工具栏。 This means that, by zooming in the first plot, the second plot should automatically resize to the same zoomed region.这意味着,通过放大第一个 plot,第二个 plot 应该自动调整到相同的缩放区域。

A way to do that could be to stack the plots one above the other, using shareX=TRUE , but I need them to be side by side.一种方法是使用shareX=TRUE将图堆叠在另一个之上,但我需要它们并排。

In python there seems to be a way to do that, using fig.update_xaxes(matches='x') .在 python 似乎有一种方法可以做到这一点,使用fig.update_xaxes(matches='x') Is there a similar option in R? R 中是否有类似的选项?

Here is a sample code:这是一个示例代码:

library(plotly) 

n = 10
x = 1:n
y = rnorm(n)

fig1 <- plot_ly(x = x, y = y, type = 'scatter', mode = 'lines+markers') 
fig2 <- plot_ly(x = x, y = y, type = 'scatter', mode = 'lines+markers') 
fig  <- subplot(fig1, fig2, shareX = TRUE) # shareX actually not working here
fig

Thank you in advance!先感谢您!

We can use matches in R just as we can in python.我们可以在 R 中使用matches ,就像在 python 中一样。

Run schema() and navigate:运行schema()并导航:

object ► layout ► layoutAttributes ► xaxis ► matches object ► layout ► layoutAttributes ► xaxis ► 匹配

for more info.了解更多信息。

This keeps all (x&y) axes synced:这使所有 (x&y) 轴保持同步:

library(plotly) 

n = 10
x = 1:n
y = rnorm(n)

fig1 <- plot_ly(x = x, y = y, type = 'scatter', mode = 'lines+markers') 
fig2 <- plot_ly(x = x, y = y, type = 'scatter', mode = 'lines+markers', xaxis = "x") %>% layout(xaxis = list(matches = "x"))
fig  <- subplot(fig1, fig2, shareX = TRUE, shareY = TRUE)
fig

结果

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

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