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
:
Plotly: How to reverse axes?
Asked Answered
You can use:
fig['layout']['yaxis']['autorange'] = "reversed"
and:
fig['layout']['xaxis']['autorange'] = "reversed"
Plot - default axis:
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:
Plot - reversed y axis:
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()
Update to 2023, this was the only solution that actually worked. –
Endblown
fig.update_yaxes(autorange="reversed", row=2, col=1)
Also works for subplots without changing the order for other plots
As a comment to this answer, when trying this an error is thrown that "row" is not recognized. Was there another setup step for this to work? Perhaps a version that has changed? It doesn't work now. –
Endblown
the row and col is for illustrating use when using in a subplot. Leave out altogether if using a single graph. –
Jozef
You can also set a range with fixed values by using the interval [hi, low] instead of using [low, hi] where (low < hi). Here are two examples.
import plotly.express
import plotly.subplots.make_subplots
fig = plotly.subplots.make_subplots(1,1)
fig.update_yaxes(range=[hi, low], row=1, col=1) # Reversed y axis because we do not use [0, H]
fig_alternate_example = plotly.express.scatter(... , range_y=(hi, low))
© 2022 - 2025 — McMap. All rights reserved.
fig.update_yaxes(autorange="reversed")
given in plotly documentation reverses the y axis for all the subplots in a row – Frederickson