Get the limits/range of the xaxis (or yaxis)

I’d like to get the limits of the xaxis but I’m struggling to find a method to do this. If fig.update_layout(xaxis=dict(range=[x1,x2])) has been called then fig.layout.xaxis.range will return the range (but not really useful because its already known).

If I try:

import plotly.graph_objects as go

fig = go.Figure()

fig.add_trace(
    go.Scatter(x=[10, 20, 40], y=[4, 6, 5,])
)
print(fig.layout.xaxis.range)

fig.show()

it returns None for print(fig.layout.xaxis.range). Is there a way to get this range for an axis?

thx!

Hi @jayveesea, in order to do this you need the Javascript figure object to communicate back to Python the value of its axis range. From Python you define the specification of a Javascript figure but after creating the figure it “lives its own life” in Javascript-land, without passing information back to Python. If you’re working in a Jupyter notebook environment you can create a FigureWidget which would print the axis range on relayout events, and register a callaback to fig.layout.on_change (like in https://plotly.com/python/click-events/ but for a different kind of events). Or you can use a Dash application with a callback listening to relayoutData.

1 Like