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()

    go.Scatter(x=[10, 20, 40], y=[4, 6, 5,])

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


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 but for a different kind of events). Or you can use a Dash application with a callback listening to relayoutData.

1 Like