Plots plotlyjs subplots: linking x axis across columns of subplots

Is it possible to link x-axis across columns of subplots with Julia plots (without linking their y-axis)?

using Plots
plotlyjs() # or pyplot()
plot(rand(100, 2), layout = (1,2), link = :x)

If I zoom or pan on one of the sub plots, the x-axis of the other subplot is not updated.

Figure_1

Looking at the documentation, the link keyword argument to plot(..) seems to only support linking x axis across rows. It seems like there should be another option to link all x axis together, something like a link = :x_all.

I know matplotlib supports this via the following (in python) using the sharex keyword argument:

fig, ax = plt.subplots(1,2, sharex=True) 

Not exactly the answer to your question, but I cannot generate the plot you want using PlotlyJS directly:

using PlotlyJS, WebIO

plot([scatter(y=rand(100), xaxis="x", yaxis="y"),
      scatter(y=rand(100), xaxis="x", yaxis="y2")],
     Layout(grid=attr(rows=2, columns=1)))

plot([scatter(y=rand(100), xaxis="x", yaxis="y"),
      scatter(y=rand(100), xaxis="x", yaxis="y2")],
     Layout(grid=attr(rows=1, columns=2)))

In the first plot, the x are linked, in the second, only the second plot appears.

I have to say that I have never got comfortable using subplots in PlotlyJS.

I got more comfortable with subplotting in PlotlyJS :grinning:.

The problem with my solution is that one cannot use grid and share the y-axis across rows: see Layout in JavaScript. The alternative is to use domain in conjunction with matches to link axes of different names. You also need to use anchor to display the y2 axis starting at the x2 axis.

using WebIO, PlotlyJS

plot([scatter(x=1:100, y=rand(100), xaxis="x", yaxis="y"),
      scatter(x=101:200, y=rand(100), xaxis="x2", yaxis="y2")],
     Layout(xaxis=attr(domain=[0, 0.45]),
            yaxis=attr(domain=[0, 1]),
            xaxis2=attr(domain=[0.55, 1], matches="x"),
            yaxis2=attr(domain=[0, 1], anchor="x2")))

Note that despite the x axes having different ranges, the displayed ranges are adjusted as to show all points.

2 Likes