Subplots and double y-axis

Hi guys. I want to show two charts, linked by their x-axis:

  • Top chart, with one trace
  • Bottom chart with two traces, each with its own y-axis

I can do it in the online creator, however Iโ€™m unable to do it from the Python API.
Am I missing a keyword?

# Imports
import numpy as np
from plotly.tools import make_subplots
import plotly.plotly as py
import plotly.graph_objs as go

# Traces
# top Trace
t = go.Scatter(
    x = x_range,
    y = x_range**2,
    yaxis = 'y1',
    name = 'T')

# bottom left-axis trace
bl = go.Scatter(
     x = x_range,
     y = x_range**0.5,
     yaxis = 'y2',
     name = "BL")

# bottom right-axis trace
br = go.Scatter(
     x = x_range,
     y = np.log(x_range+1),
     yaxis = 'y3',
     name = "BR")

# Create Fig with subplots

fig = make_subplots(rows=2, cols=1, specs=[[{}], [{}]],
                          shared_xaxes=True, shared_yaxes=False,
                          vertical_spacing=0.01)

fig.append_trace(t, 1, 1)
fig.append_trace(bl, 2, 1)
fig.append_trace(br, 2, 1)

# Layout
fig['layout']['yaxis1'].update(title='Top')
fig['layout']['yaxis2'].update(title='Bottom-left')
fig['layout']['yaxis3'].update(title='Bottom-right', side='right', overlaying='y2')

# Plot
py.iplot(fig)

Iโ€™ve added the y3 value to the y-axis attribute of the bottom right trace,

Still the result is not satisfactory as the y-3 axis (bottom right) does not command the โ€˜brโ€™ trace. See result here: https://plot.ly/~patricio.panichelli.hw/56/t-bl-br/

What am I missing? Is there anything Iโ€™m doing wrong?

Any help will be very much appreciated! Thanks!

same problem sincelast 72 hors. any answer??

Hi @sanjay,

For the usecase that @patricio.panichelli described, I would recommend layout out the axes directly, without using make_subplots.

Hereโ€™s and example based on @patricio.panichelliโ€™s example above:

# Imports
import numpy as np
from plotly.tools import make_subplots
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode()

# Traces
# top Trace
x_range = np.linspace(0, 5, 10)
t = go.Scatter(
    x = x_range,
    y = x_range**2,
    yaxis = 'y1',
    name = 'T')

# bottom left-axis trace
bl = go.Scatter(
     x = x_range,
     y = x_range**0.5,
     yaxis = 'y2',
     name = "BL")

# bottom right-axis trace
br = go.Scatter(
     x = x_range,
     y = np.log(x_range+1),
     yaxis = 'y3',
     name = "BR")

# Create Fig with subplots
layout = go.Layout(
    xaxis=go.layout.XAxis(domain=[0, 1]),
    yaxis=go.layout.YAxis(domain=[0.55, 1], anchor='free'),
    yaxis2=go.layout.YAxis(domain=[0, 0.45], anchor='x', range=[0, 3]),
    yaxis3=go.layout.YAxis(domain=[0, 0.45], anchor='x', side='right', overlaying='y2', range=[0, 2]),
)
fig = go.Figure(layout=layout)

fig.add_trace(t)
fig.add_trace(bl)
fig.add_trace(br);


# Plot
iplot(fig)

Hope that helps!
-Jon

1 Like