Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Can I use Plotly's Custom Buttons to update `shared_yaxes` in a faceted plot?

$
0
0

I am using plotly and I have a figure with subplots. I'd like to include in it a button to toggle the property shared_yaxes. Is this possible?

Here a reproducible example (in python). Consider the official simple subplot example:

from plotly.subplots import make_subplots
import plotly.graph_objects as go
fig = make_subplots(rows=1, cols=2)
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]), row=1, col=1)
fig.add_trace(go.Scatter(x=[20, 30, 40], y=[50, 60, 70]), row=1, col=2)

This produces: enter image description here

Now, you can simply use the shared_yaxes argument of make_subplots to force the same y-scale on both plots.

from plotly.subplots import make_subplots
import plotly.graph_objects as go
fig = make_subplots(rows=1, cols=2, shared_yaxes=True)
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]), row=1, col=1)
fig.add_trace(go.Scatter(x=[20, 30, 40], y=[50, 60, 70]), row=1, col=2)

And then you get:

enter image description here

Now I want to include a custom control (ideally a checkbox) to toggle that property (I've done this using R and Shiny, but now I want this plotly-based solution).

I've tried with custom buttons, for example using this code below, but I cannot make it work.

fig.update_layout(updatemenus=[
    go.layout.Updatemenu(type="buttons",
                         direction="left",
                         buttons=list([
                             dict(args=[{
                                 "shared_yaxes": True
                             }],
                                  label="Shared axes",
                                  method="relayout"),
                             dict(args=[{
                                 "shared_yaxes": False
                             }],
                                  label="Independent axes",
                                  method="relayout")
                         ]),
                         xanchor="left",
                         yanchor="top"),
])

enter image description here

Any ideas how to make it work would be much appreciated.


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>