pythonplotlydata-visualization

Plotly: How to reverse axes?


I want to reverse the y-axis of the graph I've obtained for all the over-plots using plotly.graph_objects. I know reversing of the axis can be done using plotly.express. But I would like to know how to reverse the axis using plotly.graph_objects:

Graph


Solution

  • You can use:

    fig['layout']['yaxis']['autorange'] = "reversed"
    

    and:

    fig['layout']['xaxis']['autorange'] = "reversed"
    

    Plot - default axis:

    enter image description here

    Code:

    import plotly.graph_objects as go
    import numpy as np
    
    t = np.linspace(0, 5, 200)
    y = np.sin(t)
    
    fig = go.Figure(data=go.Scatter(x=t, y=y, mode='markers'))
    
    fig.show()
    

    Plot - reversed x axis:

    enter image description here

    Plot - reversed y axis:

    enter image description here

    Code - reversed axes:

    import plotly.graph_objects as go
    import numpy as np
    
    t = np.linspace(0, 5, 200)
    y = np.sin(t)
    
    fig = go.Figure(data=go.Scatter(x=t, y=y, mode='markers'))
    fig['layout']['xaxis']['autorange'] = "reversed"
    fig['layout']['yaxis']['autorange'] = "reversed"
    
    fig.show()