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…