【问题标题】:How do I add an axis for a second trace in a Plotly subplot?如何在 Plotly 子图中为第二条轨迹添加轴?
【发布时间】:2016-12-01 22:58:45
【问题描述】:

我有三个痕迹,其中一个在一个子情节中,其中两个在另一个子情节中。我希望子图中的每条轨迹都有一个不同的 y 轴,有 2 条轨迹。

例如,我有

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)

产生

当我没有子图时,我可以获得第二条轨迹的第二个轴

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)

产生

但我不知道如何让第一个示例中的第一个子图看起来像第二个示例中的图:那里的第二个轨迹有一个不同的轴。

如何在 Plotly 子图中为第二条轨迹添加轴?

【问题讨论】:

    标签: layout plotly axes subplot multiple-axes


    【解决方案1】:

    这是一种解决方法,但似乎可行:

    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)分别:

    当跟踪附加到子图时,xaxis 和 yaxis 似乎被覆盖了,或者这就是我对this discussion 的理解。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-03
      • 2021-12-21
      • 1970-01-01
      • 2015-03-20
      • 2016-08-16
      相关资源
      最近更新 更多