【问题标题】:How to add secondary xaxis in plotly using plotly.express? [duplicate]如何使用 plotly.express 在 plotly 中添加辅助轴? [复制]
【发布时间】:2021-08-07 21:10:08
【问题描述】:

我只找到了 yaxis 的示例。跟着它,我得到一个错误:

Invalid key specified in an element of the 'specs' argument to make_subplots: 'secondary_x'
    Valid keys include: ['type', 'secondary_y', 'colspan', 'rowspan', 'l', 'r', 'b', 't']

我的代码如下:

import pandas as pd
import plotly.express as px
import plotly.graph_objects as go

from plotly.subplots import make_subplots

## sample DataFrames
df1=pd.DataFrame({'A':[1,2,3],'B':[4,5,6]})
df2=pd.DataFrame({'x':[1,2,3],'y':[7,8,9]})

fig = px.scatter(df1, x='A', y='B')
fig.add_scatter(x=df2['x'], y=df2['y'])

fig.update_traces(name='Points', showlegend = True)


# Fake figure due to axis
subfig = make_subplots(specs=[[{"secondary_x": True}]])
df2=pd.DataFrame({'C':[10,20,30],'D':[40,50,60]})
fig2 = px.scatter(df2, x='C', y='D')
fig2.update_traces(xaxis="x2")
subfig.add_traces(fig.data + fig2.data)

fig.show()

建议后

代码包含:

fig.update_layout(legend=dict(
    yanchor="top",
    y=0.99,
    xanchor="left",
    x=0.83,
    font=dict(
        family="Trebuchet",
        size=20,
        color="black"
     ),
        bgcolor="LightGray",
        bordercolor="Black",
        borderwidth=0
))

是否可以将这个和 fig.layout = layout 结合起来?

【问题讨论】:

    标签: python-3.x plotly plotly-express


    【解决方案1】:

    Plotly 中的 make_subplots 方法不支持辅助 x 轴。但是,您可以使用 XAxisYAxis 方法在 layout 中自己创建辅助 x 轴,您可以使用以下方法导入:from plotly.graph_objs.layout import YAxis,XAxis

    然后你将fig.layout设置为你创建的布局,当你想使用辅助x轴和辅助y轴时,将它们作为参数传递给go.Scatter

    在您的情况下,我认为您自己创建辅助 y 轴和辅助 x 轴看起来更简洁,这也消除了使用 make_subplots 方法的需要。

    更新:您可以在布局中添加其他参数来格式化您的图例。

    import pandas as pd
    import plotly.express as px
    import plotly.graph_objects as go
    from plotly.graph_objs.layout import YAxis,XAxis
    
    ## sample DataFrames
    df1=pd.DataFrame({'A':[1,2,3],'B':[4,5,6]})
    df2=pd.DataFrame({'x':[1,2,3],'y':[7,8,9]})
    
    ## sample DataFrame with different x-axis and y-axis
    df3=pd.DataFrame({'C':[10,20,30],'D':[40,50,60]})
    
    ## add a secondary xaxis and yaxis in the layout
    layout = go.Layout(
        xaxis2 = XAxis( 
            overlaying='x',
            side='top',
        ),
        yaxis2=YAxis(
            overlaying='y',
            side='right',
        ),
        legend=dict(
            yanchor="top",
            y=0.99,
            xanchor="left",
            x=0.83,
            font=dict(
                family="Trebuchet",
                size=20,
                color="black"
             ),
                bgcolor="LightGray",
                bordercolor="Black",
                borderwidth=0
        )
    )
    
    fig = px.scatter(df1, x='A', y='B')
    fig.add_scatter(x=df2['x'], y=df2['y'])
    fig.update_traces(name='Points', showlegend = True)
    
    ## set the layout to be the figure layout
    fig.layout = layout
    
    ## add the final trace specifying the secondary x-axis and secondary y-axis
    fig.add_trace(go.Scatter(x=df3['C'], y=df3['D'], xaxis='x2', yaxis='y2'))
    
    fig.show()
    

    【讨论】:

    • 非常感谢。我的原始代码还包括 fig.update_layout (我编辑了我的问题)。是否可以将其与 fig.layout = layout 结合使用
    • 是的,您应该能够在定义layout 时将这些相同的参数传递给go.Layout。可以查看文档here查看go.Layout可以带的参数
    • 更新了我的答案以显示如何添加其他布局参数,例如图例属性
    • 谢谢,我已经用 fig = px.scatter() 和 fig.add_scatter() 绘制了几组数据,这可以和 go 结合吗?
    猜你喜欢
    • 1970-01-01
    • 2020-11-01
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 2020-11-01
    • 2021-07-14
    • 2022-06-14
    • 2020-11-22
    相关资源
    最近更新 更多