Plotly. I have problem with buttons to change Yaxis.type. When someone clicked on same button twice or more yaxis is scaling all the time

Viewed 43

I have problem with buttons to change Yaxis.type. When someone clicked on same button twice or more yaxis is scaling all the time. As a result my chart looks like this: after few clicks on same button but i would like to have it like this: correct version

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()
1 Answers
  • have built a reproducible example of question
  • there is definitely an issue with figure and frame layout interactions
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"),
                ]
            ),
        ),
    ],
)



Related