使用多个子图更改 y 轴(线性、对数、sqrt)的设置按钮,Plotly

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

提问人:Leo 提问时间:2/19/2021 最后编辑:Manoj Babu G RLeo 更新时间:2/19/2021 访问量:265

问:

我想设置一个按钮来更改 3 个子图的 Y 轴,介于线性、对数和 sqrt 之间。

根据 @vestland 对我上一个问题的回答:将 sqrt 设置为下拉列表或按钮 python/Plotly 的 yaxis 比例,我有一个工作解决方案,为每个子图(lin、log、sqrt)绘制 3 行,按钮控件是可见的。

但是,下载的情节的 HTML 不是很被动,所以我在想:

是否最好将 y.axis.type 用于对数和线性,并且每个子图仅使用 visible(True, False)。

下面是一个工作示例代码,它使用控制链接的按钮制作 3 个子图

#@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()
麻木 按钮 情节 剧情

评论


答: 暂无答案