2016-07-27 321 views
2

我有三條痕跡,其中一條在一個小區裏,另外兩條在另一個小區裏。我想要有一個獨特的Y軸,每個子曲線有2條曲線。如何在Plotly子圖中爲第二條曲線添加軸?

例如,我有

fig = plotly.tools.make_subplots(rows=2, cols=1, shared_xaxes=True) 
fig.append_trace(trace1, 1, 1) 
fig.append_trace(trace2, 1, 1) 
fig.append_trace(trace3, 2, 1) 
fig['layout'].update(height=200, width=400) 

產生

enter image description here

而當我沒有副區,我可以得到所述第二跡線的第二軸線與

layout = go.Layout(
    yaxis=dict(
     title='y for trace1' 
    ), 
    yaxis2=dict(
     title='y for trace2', 
     titlefont=dict(
      color='rgb(148, 103, 189)' 
     ), 
     tickfont=dict(
      color='rgb(148, 103, 189)' 
     ), 
     overlaying='y', 
     side='right' 
    ) 
) 
fig = go.Figure(data=data, layout=layout) 

哪產生

enter image description here

但我無法弄清楚如何獲得第一次要情節在第一個例子看起來像在第二個例子中的情節:與第二跡有明顯的軸線。

如何在Plotly子圖中爲第二條曲線添加軸?

回答

0

這是一個有點變通方法,但它似乎工作:

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) * 10, y = np.random.randn(1000) * 10, yaxis = "y3", 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(range = [0, 3], anchor = "x1", side = "left") 
fig["layout"]["yaxis2"].update(range = [0, 3], anchor = "x2", side = "left") 
fig["layout"]["yaxis3"].update(range = [0, 30], anchor = "x2", side = "right", overlaying = "y2") 

py.offline.plot(fig) 

產生以下,其中trace0是第一次要情節繪製yaxis1,並且trace1trace2在第二插曲,繪製分別yaxis2(0-3)和yaxis3(0-30): enter image description here

當痕跡被附加到次要情節,x軸和y軸似乎被覆蓋,或者說是我的理解無論如何,this discussion

相關問題