Plotly: How to reverse axes?
Asked Answered
P

3

14

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

Penner answered 29/11, 2019 at 6:58 Comment(0)
T
13

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()
Taliataliaferro answered 29/11, 2019 at 8:17 Comment(2)
Awesome, this works for a particular subplot as well. Whereas fig.update_yaxes(autorange="reversed") given in plotly documentation reverses the y axis for all the subplots in a rowFrederickson
Update to 2023, this was the only solution that actually worked.Endblown
J
7
fig.update_yaxes(autorange="reversed", row=2, col=1)

Also works for subplots without changing the order for other plots

Jozef answered 10/5, 2021 at 13:39 Comment(2)
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
F
0

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))
Foot answered 26/7, 2024 at 4:3 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.