Set linear or log axes from button or drop-down menu - Python

Hi,
I’m trying to have a drop down menu to set one of the axis to be either linear or logarithmic.
Ideally what I do is to update the figure properties when of the button is pressed.

import plotly.graph_objects as go

fig = go.Figure()

fig.add_trace(go.Scatter(
    x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
    y=[8, 7, 6, 5, 4, 3, 2, 1, 0]
))

fig.add_trace(go.Scatter(
    x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
    y=[0, 1, 2, 3, 4, 5, 6, 7, 8]
))

fig.update_layout(title_text="CIR plot ",
                          updatemenus=[
            dict(
                 buttons=list([
                     dict(label="Linear",  method="update", args=[{"yaxis":{"type": "linear"}}]),
                     dict(label="Log", method="update", args=[{"yaxis":{"type": "log"}}]),
                                  ]),
            )])
# fig.update_layout(xaxis_type="log", yaxis_type="log")
fig.show()

Hello ,
I am trying to add dropdown menu that changes yaxis from linear to log. Tried the code above but did not help. Did you find another way?

Thanks !

@mhmtsrhn22

The button definitions should be modified as follows:

fig.update_layout(title_text="CIR plot ",
                          updatemenus=[
            dict(
                 buttons=list([
                     dict(label="Linear",  
                          method="relayout", 
                          args=[{"yaxis.type": "linear"}]),
                     dict(label="Log", 
                          method="relayout", 
                          args=[{"yaxis.type": "log"}]),
                                  ]),
            )])

The method "update" is to be replaced by "relayout", because the latter is more suitable:

"restyle": modify data or data attributes
"relayout": modify layout attributes
"update": modify data and layout attributes

and the dict: {'yaxis':{'type': 'log'}}
by:
{"yaxis.type": "log"}

2 Likes