Search code examples
pythonrgraphplotlyaxes

Plotly: How to set zeroline opacity?


Is there a way to change the opacity of the zeroline in plotly? I am trying to layer the zeroline for my y-axis over my plot so it does not appear behind, but also have opacity/alpha of 50%. Is this possible?


Solution

  • You can use an rgba color together with fig.update_xaxes() and /or fig.update_yaxes()to get any color with any opacity you would like:

    enter image description here

    Complete code:

    import plotly.express as px
    
    fig = px.line(y=[1, -1], x=[-1,1])
    
    fig.update_xaxes(zeroline=True, zerolinewidth=6, zerolinecolor='rgba(0,255,0,0.5)')
    fig.update_yaxes(zeroline=True, zerolinewidth=6, zerolinecolor='rgba(0,0,255,0.5)')
    
    fig.show()