Subplots with logscale and two y axes

I’m trying to make a grid of subplots, each with

  • a log y axis on the left
  • a log x axis
  • a normal y axis on the right

I dig quite a lot in the documentation, but I can’t find a solution:

How to make subplots is shown here using make_subplots and fig.append_trace.

How two have two y axes is shown here using go.Layout and go.Figure

How can I mix the two?

I always find very confusing how to tell figure which data has to go in each axes. Moreover I never found a good documentation for layout.

Could you point me in the right direction?

Thank you
Luca

Hi @LucaA,

I don’t think make_subplots can handle creating subplots with multiple y-axis. So we’ll need to manually define the axes. Let’s start with a single subplot with two y-axes, one linear one log (Note I started with this example: https://plot.ly/python/multiple-axes/).

from plotly.offline import init_notebook_mode, iplot
import plotly.graph_objs as go
init_notebook_mode()

trace1 = go.Scatter(
    x=[1, 2, 3],
    y=[2, 32, 62],
    name='yaxis data'
)
trace2 = go.Scatter(
    x=[1, 2, 3],
    y=[40, 50, 60],
    name='yaxis2 data',
    yaxis='y2'
)
data = [trace1, trace2]
layout = go.Layout(
    title='Double Y Axis Example',
    xaxis=dict(
        title='xaxis title',
    ),
    yaxis=dict(
        title='yaxis title',
        type='log'
    ),
    yaxis2=dict(
        title='yaxis2 title',
        overlaying='y',
        side='right'
    )
)
fig = go.Figure(data=data, layout=layout)
iplot(fig)

Now, we can position the as a subplot using the axis domain property

from plotly.offline import init_notebook_mode, iplot
import plotly.graph_objs as go
init_notebook_mode()

trace1 = go.Scatter(
    x=[1, 2, 3],
    y=[2, 32, 62],
    name='yaxis data',
)
trace2 = go.Scatter(
    x=[1, 2, 3],
    y=[40, 50, 60],
    name='yaxis2 data',
    yaxis='y2'
)
data = [trace1, trace2]
layout = go.Layout(
    title='Double Y Axis Example',
    xaxis=dict(
        domain=[0, 0.4]
    ),
    yaxis=dict(
        title='yaxis title',
        type='log',
        domain=[0.6, 1.0]
    ),
    yaxis2=dict(
        title='yaxis2 title',
        overlaying='y',
        side='right'
    )
)
fig = go.Figure(data=data, layout=layout)
iplot(fig)

Finally, repeat the process 4 times to make a 2x2 subplot grid

from plotly.offline import init_notebook_mode, iplot
import plotly.graph_objs as go
init_notebook_mode()

# Top left
trace1 = go.Scatter(
    x=[1, 2, 3],
    y=[2, 32, 62],
    name='yaxis data',
)
trace2 = go.Scatter(
    x=[1, 2, 3],
    y=[40, 50, 60],
    name='yaxis2 data',
    yaxis='y2'
)

# Top right
trace3 = go.Scatter(
    x=[1, 2, 3],
    y=[2, 32, 62],
    name='yaxis3 data',
    xaxis='x2',
    yaxis='y3'
)
trace4 = go.Scatter(
    x=[1, 2, 3],
    y=[40, 50, 60],
    name='yaxis4 data',
    xaxis='x2',
    yaxis='y4'
)

# Bottom left
trace5 = go.Scatter(
    x=[1, 2, 3],
    y=[2, 32, 62],
    name='yaxis5 data',
    xaxis='x3',
    yaxis='y5'
)
trace6 = go.Scatter(
    x=[1, 2, 3],
    y=[40, 50, 60],
    name='yaxis6 data',
    xaxis='x3',
    yaxis='y6'
)

# Bottom right
trace7 = go.Scatter(
    x=[1, 2, 3],
    y=[2, 32, 62],
    name='yaxis7 data',
    xaxis='x4',
    yaxis='y7'
)
trace8 = go.Scatter(
    x=[1, 2, 3],
    y=[40, 50, 60],
    name='yaxis8 data',
    xaxis='x4',
    yaxis='y8'
)


data = [trace1, trace2, trace3, trace4, trace5, trace6, trace7, trace8]
layout = go.Layout(
    title='Double Y Axis Example',
    legend={'x': 1.1},
    width=1000,
    height=500,
    # Top left
    xaxis=dict(
        title='xaxis title',
        domain=[0, 0.4]
    ),
    yaxis=dict(
        title='yaxis title',
        type='log',
        domain=[0.6, 1.0],
        anchor='x'
    ),
    yaxis2=dict(
        title='yaxis2 title',
        overlaying='y',
        side='right'
    ),
    
    # Top right
    xaxis2=dict(
        title='xaxis2 title',
        domain=[0.6, 1.0],
        anchor='y3'
    ),
    yaxis3=dict(
        type='log',
        title='yaxis3 title',
        domain=[0.6, 1.0],
        anchor='x2'
    ),
    yaxis4=dict(
        title='yaxis4 title',
        domain=[0.6, 1.0],
        overlaying='y3',
        side='right',
        anchor='x2'
    ),
    
    # Bottom left
    xaxis3=dict(
        title='xaxis3 title',
        domain=[0, 0.4],
        anchor='y5'
    ),
    yaxis5=dict(
        type='log',
        title='yaxis5 title',
        domain=[0, 0.4],
        anchor='x3'
    ),
    yaxis6=dict(
        title='yaxis6 title',
        domain=[0, 0.4],
        overlaying='y5',
        side='right',
        anchor='x3'
    ),
    
    # Bottom right
    xaxis4=dict(
        title='xaxis4, title',
        domain=[0.6, 1.0],
        anchor='y7'
    ),
    yaxis7=dict(
        type='log',
        title='yaxis7 title',
        domain=[0, 0.4],
        anchor='x4'
    ),
    yaxis8=dict(
        title='yaxis8 title',
        domain=[0, 0.4],
        overlaying='y7',
        side='right',
        anchor='x4'
    )
)
fig = go.Figure(data=data, layout=layout)
iplot(fig)

Hope that helps!
-Jon

1 Like