Subplots and double y-axis

#1

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!

#2

same problem sincelast 72 hors. any answer??

#3

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