Search code examples
pythonplotlylinechart

Y axes labels on the right as well in Plotly


I'm just trying to add the same Y axes labels (4040,4050 etc..) on the right as well, on a Plotly chart. To make it simple, just mirror it.

This is a Plotly line chart.

This is quite simple to do on a matplotlib by using plt.tick_params(labeltop=False,labelright=True)

I'm using (import plotly.express as px)

Just wondering if there was a straightforward way to do it on Plotly Line and Area chart as well? enter image description here

Thank you


Solution

  • Have used approach of duplicating trace and setting it to use second yaxis.

    import plotly.express as px
    
    df = px.data.stocks()
    fig = px.line(df, x="date", y="GOOG")
    
    # add another trace to carry second y-axis
    fig.add_traces({**fig.to_dict()["data"][0], **{"yaxis": "y2"}})
    # config second yaxis
    fig.update_layout(
        {
            "yaxis2": {
                "side": "right",
            }
        }
    )
    

    enter image description here