Add button to change from linear scale to log scale

Hey all, based on answers to some other questions I was trying to plot a time series where I can switch from linear to log scale on the y axis. It seems the way to go is using updatemenus, so I tried this:

updatemenus = [
    dict(
        type="buttons",
        direction="left",
        buttons=list([
            dict(
                args=[{'yaxis': {'type': 'linear'}}],
                label="Linear Scale",
                method="update"
            ),
            dict(
                args=[{'yaxis': {'type': 'log'}}],
                label="Log Scale",
                method="update"
            )
        ])
    ),
]  

    fig = px.line(df, x='date', y='total_cases', color='country') #Time series for all countries in my table

    fig.update_layout(
        updatemenus=updatemenus
    )

This brings me this plot:

enter image description here

Which looks fine, but, when I press log scale, the plot remains the same:

enter image description here

What am I missing? I guess it’s something simple. Any help would be kindly appreciated

1 Like

@Juanouo
You have to use the method “relayout” and as args:
[{"yaxis.type": "log"}]
The reason why you have to change your definition is explained here: https://plotly.com/~empet/15608.
The first example in this tutorial is just switching from linear to log scale.

3 Likes

Thanks a lot, @empet, my brain was burning and I couldn’t find any help for my problem. Worked like a charm !

@empet In order to switch between linear and log scale, we need to used updatemenus, which results in having buttons by side of the graph. I would like to keep two separate button in my dashboard, and using them I want to switch between scales. Or may be in someway move the buttons from side of the graph to above them, making part them of my dashboard layout. How do we achieve this? Thanks in advance