Setting button that changes y axis (linear, log, sqrt) with multiple subplots, Plotly

Viewed 203

I want to set a button that changes the Y-axis for 3 subplots, between linear, log, and sqrt.

From @vestland 's answer to my previous question: Set sqrt as yaxis scale from dropdown or button-Python/Plotly, I have a working solution that plots 3 lines for each subplot (lin, log, sqrt) and the button controls which is visible.

However, the HTML of the plot downloaded isn't very reactive, so I was thinking:

Is it better to use y.axis.type for log and linear, and only the visible(True, False) for each subplot.

Here is the working example code that makes 3 subplots with a button controlling linking

#@title EXAMPLE SQRT axis change { form-width: "20px" }
import numpy as np
import plotly.graph_objects as go
import plotly.express as px
from plotly.subplots import make_subplots
df = px.data.gapminder().query("year == 2002 or year == 2007 ")
 #3 subplots with one legend linking all
fig = make_subplots(1, 3)
for country, grp in df.groupby (["country"]):
  grp1= grp.query("year == 2002 ")
  fig.add_trace(go.Scatter(x=grp1["gdpPercap"], y=grp1["lifeExp"], legendgroup=country, visible = True,mode='markers', name = country),   col=1, row=1)
  fig.add_trace(go.Scatter(x=grp1["gdpPercap"], y=np.sqrt(grp1["lifeExp"]), legendgroup=country, visible = False, mode='markers', name = country),   col=1, row=1)
  fig.add_trace(go.Scatter(x=grp1["gdpPercap"], y=np.log10(grp1["lifeExp"]), legendgroup=country, visible = False, mode='markers', name = country),   col=1, row=1)
   #adding a second plot that has the same labels and from legend are selected togheter.
  grp2= grp.query("year == 2007 ")
  fig.add_trace(go.Scatter(x=grp2["gdpPercap"], y=grp2["lifeExp"], legendgroup=country, visible = True,mode='markers',name = country, showlegend=False),   col=2, row=1)
  fig.add_trace(go.Scatter(x=grp2["gdpPercap"], y=np.sqrt(grp2["lifeExp"]), legendgroup=country, visible = False, mode='markers',name = country, showlegend=False),   col=2, row=1)
  fig.add_trace(go.Scatter(x=grp2["gdpPercap"], y=np.log10(grp2["lifeExp"]), legendgroup=country, visible = False, mode='markers',name = country,showlegend=False),   col=2, row=1)
  
   #Adding third plot
  fig.add_trace(go.Scatter(x=grp2["gdpPercap"], y=grp2["pop"], legendgroup=country, visible = True,mode='markers',name = country, showlegend=False),   col=3, row=1)
  fig.add_trace(go.Scatter(x=grp2["gdpPercap"], y=np.sqrt(grp2["pop"]), legendgroup=country, visible = False, mode='markers',name = country, showlegend=False),   col=3, row=1)
  fig.add_trace(go.Scatter(x=grp2["gdpPercap"], y=np.log10(grp2["pop"]), legendgroup=country, visible = False, mode='markers',name = country,showlegend=False),   col=3, row=1)

# buttons for updatemenu
buttons = [dict(method='restyle',
                label='linear',
                visible=True,
                args=[{'label': 'linear',
                       'visible':[True, False, False],
                      }
                     ]),
           dict(method='restyle',
                label='log',
                visible=True,
                args=[{'label': 'log',
                       'visible':[False, False, True],
                      }
                     ]),
           dict(method='update',
                label='sqrt',
                visible=True,
                args=[{'label': 'linear',
                       'visible':[False, True, False],
                      }
                     ])]
           
# specify updatemenu        
um = [{'buttons':buttons,
      'direction': 'down'}
      ]

fig.update_layout(updatemenus=um)
fig.show()
0 Answers
Related