# Subplot with multiple y axis

Hi,
I am trying to plot a graph with two subplots when one of them has two y axis.

I found the following solution, but it works only with two traces in the same subplot, when increasing the number of traces it fails…

import plotly as py
import plotly.graph_objs as go
from plotly import tools
import numpy as np

left_trace = go.Scatter(x = np.random.randn(1000), y = np.random.randn(1000), yaxis = “y1”, mode = “markers”)
right_traces = []
right_traces.append(go.Scatter(x = np.random.randn(1000), y = np.random.randn(1000), yaxis = “y2”, mode = “markers”))
right_traces.append(go.Scatter(x = np.random.randn(1000), y = np.random.randn(1000)+10, yaxis = “y4”, mode = “markers”))

fig = tools.make_subplots(rows = 1, cols = 2)
fig.append_trace(left_trace, 1, 1)
for trace in right_traces:
yaxis = trace[“yaxis”] # Store the yaxis
fig.append_trace(trace, 1, 2)
fig[“data”][-1].update(yaxis = yaxis) # Update the appended trace with the yaxis

fig[“layout”][“yaxis1”].update(anchor = “x1”, side = “left”)
fig[“layout”][“yaxis2”].update(anchor = “x2”, side = “left”)
fig[“layout”][“yaxis4”].update(anchor = “x2”, side = “right”, overlaying = “y2”)

py.offline.plot(fig)

When I add a third trace to the second subplot this solution fails.

import plotly as py
import plotly.graph_objs as go
from plotly import tools
import numpy as np

left_trace = go.Scatter(x = np.random.randn(1000), y = np.random.randn(1000), yaxis = “y1”, mode = “markers”)
right_traces = []
right_traces.append(go.Scatter(x = np.random.randn(1000), y = np.random.randn(1000), yaxis = “y2”, mode = “markers”))
right_traces.append(go.Scatter(x = np.random.randn(1000), y = np.random.randn(1000)+ 5, yaxis = “y2”, mode = “markers”))
right_traces.append(go.Scatter(x = np.random.randn(1000), y = np.random.randn(1000)+10, yaxis = “y4”, mode = “markers”))

fig = tools.make_subplots(rows = 1, cols = 2)
fig.append_trace(left_trace, 1, 1)
for trace in right_traces:
yaxis = trace[“yaxis”] # Store the yaxis
fig.append_trace(trace, 1, 2)
fig[“data”][-1].update(yaxis = yaxis) # Update the appended trace with the yaxis

fig[“layout”][“yaxis1”].update(anchor = “x1”, side = “left”)
fig[“layout”][“yaxis2”].update(anchor = “x2”, side = “left”)
fig[“layout”][“yaxis3”].update(anchor = “x2”, side = “left”)
fig[“layout”][“yaxis4”].update(anchor = “x2”, side = “right”, overlaying = “y2”)

py.offline.plot(fig)

I am a bit lost as to how to solve this…

@cobym Recently I answered a similar question on this forum, and explained how to handle information for each cell in this jupyter notebook https://plot.ly/~empet/14352.

Thanks!
I still have problems with it - I rewrote the code with using your answer in the link you posted.
The problem is that when I plot the graph one of the sublots is empty. While when I press the “Export plot.ly” button I see that subplot correctly:
auto_open plot:

export to plot.ly:

Do you have idea what is the cause of this?

from plotly import tools
import plotly.graph_objs as go
from plotly.offline import plot

x = [0, 2, 3, 4, 5, 6, 7, 9]
y1 = [0, 2, 3, 4, 5, 6, 7, 9]
y2 = [10, 14, 16, 18, 20, 22, 24, 28]
y3 = [10, 8, 7, 6, 5, 4, 3, 1]
y4 = [25, 21, 19, 17, 15, 13, 11, 7]
y5 = [25, 21, 19, 17, 15, 13, 11, 7]
y6 = [25, 21, 19, 17, 15, 13, 11, 7]
title_list = [‘a’, ‘b’, ‘c’]

fig = tools.make_subplots(rows=2, cols=2, print_grid=True, horizontal_spacing=0.18, subplot_titles=title_list)

trace1 = go.Scatter(x=x, y=y1, mode=‘lines’, name=‘y1’, showlegend=False)

trace2 = go.Scatter(x=x, y=y2, mode=‘lines’, name=‘y2’, showlegend=False)

trace3 = go.Scatter(x=x, y=y1, mode=‘lines’, name=‘y3’, showlegend=False)

trace4 = go.Scatter(x=x, y=y3, mode=‘lines’, name=‘y3’, showlegend=False)

trace5 = go.Scatter(x=x, y=y5, mode=‘lines’, name=‘y4’, showlegend=False)

trace6 = go.Scatter(x=x, y=y4, mode=‘lines’, name=‘y4’, showlegend=False)

fig.append_trace(trace1, 1, 1)
fig.append_trace(trace4, 1, 1)
fig.append_trace(trace2, 1, 2)
fig.append_trace(trace5, 1, 2)
fig.append_trace(trace3, 2, 1)
fig.append_trace(trace6, 2, 1)

fig[‘data’].update(yaxis=‘y4’)
fig[‘data’].update(yaxis=‘y5’)
fig[‘data’].update(yaxis=‘y6’)

fig[‘layout’].update(autosize=False, width=800, height=500, showlegend=False, hovermode=‘x’)

fig[‘layout’][‘yaxis1’].update(range=[0, 9], showgrid=True, title=‘y1’)

fig[‘layout’][‘yaxis2’].update(range=[10, 28], showgrid=True, title=‘y2’)

fig[‘layout’][‘yaxis3’].update(range=[0, 9], showgrid=True, title=‘y3’)

fig[‘layout’][‘yaxis4’] = dict(range=[1, 10], overlaying=‘y1’, anchor=‘x1’, side=‘right’, showgrid=False, title=‘y4’)

fig[‘layout’][‘yaxis5’] = dict(range=[7, 28], overlaying=‘y2’, anchor=‘x2’, side=‘right’, showgrid=False, title=‘y5’)

fig[‘layout’][‘yaxis6’] = dict(range=[7, 25], overlaying=‘y3’, anchor=‘x3’, side=‘right’, showgrid=False, title=‘y6’)

plot(fig, filename=’/home/…/test.html’, auto_open=True)

1 Like

@cobym I ran your code and it worked.

`pip install plotly --upgrade`