Python Plotly: make secondary axis log scale

3k views Asked by At

I have a dataset x,y1,y2. Using Plotly, I want to:

  • Plot y1 on axis 1: linear
  • Plot y2 on axis2: log

I imagine it would look somehting like this:

fig.update_layout( yaxis_type="log", secondary_y=True) #A command like this one

This is my setup so far

import pandas as pd

import plotly.express as px
from plotly.subplots import make_subplots
import plotly.graph_objects as go


df= pd.DataFrame({'x': [0,1,2,3,4], 'y1' : [2,4,6,8,10], 'y2': [5,10,20,30,35]})

fig = make_subplots(specs=[[{"secondary_y": True}]])

fig.add_trace(
    go.Scatter(x=df["x"], y=df["y1"], name='normal scale'),
    secondary_y=False
)


fig.add_trace(
    go.Scatter(x=df["x"], y=df["y2"], name='make this log scale'),
    secondary_y=True,
)
fig.update_layout( yaxis_type="log", secondary_y=True) #A command like this one
fig.show()
1

There are 1 answers

6
r-beginners On BEST ANSWER

Name the other y-axis and set up a 'log' for it. I used this information as a reference.

import pandas as pd

import plotly.express as px
from plotly.subplots import make_subplots
import plotly.graph_objects as go


df= pd.DataFrame({'x': [0,1,2,3,4], 'y1' : [2,4,6,8,10], 'y2': [5,10,20,30,35]})

fig = make_subplots(specs=[[{"secondary_y": True}]])

fig.add_trace(
    go.Scatter(x=df["x"], y=df["y1"], name='normal scale'),
    secondary_y=False
)


fig.add_trace(
    go.Scatter(x=df["x"], y=df["y2"], name='make this log scale', yaxis='y2'), # update
    secondary_y=True,
)
fig.update_layout(
                     yaxis2=dict(type='log')
                     ) # update
fig.show()

enter image description here