阴谋.我对更改Yaxis.type的按钮有问题.当有人点击同一个按钮两次或两次以上时,Yaxis一直在缩放



我对更改Yaxis.type的按钮有问题。当有人点击同一按钮两次或两次以上时,Yaxis一直在缩放。因此,我的图表如下所示:在同一个按钮上点击几下之后但我希望它是这样的:正确版本

trace1 = go.Scatter(x=df['time'][:2],
y=df['cumsum'][:2],
fill='tozeroy'
)
frames = [dict(data= [dict(type='scatter',
x=df['time'][:k+1],
y=df['cumsum'][:k+1]),
],
traces= [0],  
)for k  in  range(1, len(df)-1)]
layout = go.Layout(width=700,
height=600,
showlegend=False,
hovermode='x unified',
updatemenus=[
dict(
type='buttons', showactive=False,
y=1.05,
x=1.15,
xanchor='right',
yanchor='top',
pad=dict(t=0, r=10),
buttons=[dict(label='Play',
method='animate',
args=[None, 
dict(frame=dict(duration=1.5, 
redraw=False),
transition=dict(duration=0),
fromcurrent=True,
mode='immediate')]
)]
),
dict(
type = "buttons",
direction = "left",
buttons=list([
dict(
args=[{"yaxis.type": "linear"}],
label="LINEAR",
method="relayout"
),
dict(
args=[{"yaxis.type": "log"}],
label="LOG",
method="relayout"
)
]),
),
]              
)
layout.update(title = "How many messages with your friend",
xaxis =dict(range=[start_date,end_date], autorange=False),
yaxis =dict(range=[0, max(df["cumsum"])], autorange=False));
fig = go.Figure(data=[trace1], frames=frames, layout=layout)
fig.show()
  • 构建了一个可复制的问题示例
  • 图形和框架布局交互肯定存在问题
import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
df = pd.DataFrame({"time": range(2014, 2021), "cumsum": np.geomspace(10, 75000, 7)})
fig = px.line(
pd.concat(
[
df.loc[df["time"].le(y + 1)].assign(step=y)
for y in df["time"]
if y < df["time"].max()
]
),
x="time",
y="cumsum",
animation_frame="step",
).update_traces(fill="tozeroy")
fig.frames = [
f.update(
layout={
"xaxis": {"range": [f.data[0].x[0], f.data[0].x[-1]]},
# "yaxis": {"range": [0, df["cumsum"].max()]},
}
)
for f in fig.frames
]

fig.update_layout(
xaxis={"dtick": 1},
updatemenus=fig.to_dict()["layout"]["updatemenus"]
+ [
dict(
type="buttons",
direction="left",
y=2.0,
buttons=list(
[
dict(
args=[{"yaxis.type": "linear"}],
label="LINEAR",
method="relayout",
),
dict(args=[{"yaxis.type": "log"}], label="LOG", method="relayout"),
]
),
),
],
)