【问题标题】:Plotly/Dash: is it possible to hide tick labels of a secondary y axis?Plotly/Dash:是否可以隐藏辅助 y 轴的刻度标签?
【发布时间】:2021-11-23 22:48:33
【问题描述】:

我想从辅助 y 轴上移除刻度标签,同时保留主要 y 轴上的刻度标签。

    fig.add_trace(go.Scatter(x=df.index, y=df['A'],
                             line=dict(color='rgba(255, 255, 0, 0.25)', width=1)), secondary_y=False, row=1, col=1)
    fig.add_trace(go.Scatter(x=df.index, y=df['B'],
                             line=dict(color='rgba(128, 75, 183, 0.5)', width=1)), secondary_y=True, row=1, col=1)
    fig.update_yaxes(showticklabels=True, row=1, col=1, secondary_y=False)

以上并没有解决问题,因为次要 y 轴的刻度标签仍然可见。

有什么想法吗?

【问题讨论】:

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


    【解决方案1】:

    你可以使用:

    fig.layout.yaxis2.update(showticklabels=False)
    

    得到:

    否则你会产生:

    完整代码:

    import plotly.graph_objects as go
    from plotly.subplots import make_subplots
    
    # Create figure with secondary y-axis
    fig = make_subplots(specs=[[{"secondary_y": True}]])
    
    # Add traces
    fig.add_trace(
        go.Scatter(x=[1, 2, 3], y=[40, 50, 60], name="yaxis data"),
        secondary_y=False,
    )
    
    fig.add_trace(
        go.Scatter(x=[2, 3, 4], y=[4, 5, 6], name="yaxis2 data"),
        secondary_y=True,
    )
    
    # Add figure title
    fig.update_layout(
        title_text="Double Y Axis Example"
    )
    
    # Set x-axis title
    fig.update_xaxes(title_text="xaxis title")
    
    # Set y-axes titles
    fig.update_yaxes(title_text="<b>primary</b> yaxis title", secondary_y=False)
    fig.update_yaxes(title_text="<b>secondary</b> yaxis title", secondary_y=True)
    fig.layout.yaxis2.update(showticklabels=False)
    
    fig.show()
    

    【讨论】:

    • 非常感谢@vestland!
    • @pepe 不客气!并感谢您接受我的建议。
    猜你喜欢
    • 2020-11-22
    • 1970-01-01
    • 1970-01-01
    • 2016-08-30
    • 1970-01-01
    • 2020-03-30
    • 2021-12-11
    • 2015-12-18
    • 1970-01-01
    相关资源
    最近更新 更多