Black Lives Matter. Please consider donating to Black Girls Code today.
Learn how to use COVID-19 data in open source Dash apps. Register for the Sept 23rd webinar with IQT!

Retrieving the range of the x-axis of a zoomed in plot

I’m trying to retrieve the range of the x-axis when you zoom in a plotly scatter plot, but when I tried to access it using:

figure['layout']['xaxis']['range'] 

it just returns ‘None’.
This is the code that I am using to create the graph:

# self.plot contains the dataframe passed to the function to be plotted
self.plot = df
# Creates the plotly plot figure
self.fig = self.plot.iplot(asFigure=True,kind='scatter', xTitle='Date', yTitle='Temperature')
# Displays the plot
iplot(self.fig, show_link=False)

I’m using cufflinks to create the plot from a pandas DataFrame so I don’t explicitly set the layout.xaxis.range to anything. Also I’m using a Jupyter Notebook to display the graph, if that helps at all. So is there any way of getting the range of the x-axis of the current view window of the plot? Thanks in advance!

Graph parameters don’t dynamically change with chart actions. However, as you zoom, the plot does emit data regarding the new x-axis range, you’re unable to to access this information directly in Python. Instead you’d have to use javascript: https://plot.ly/javascript/zoom-events/

1 Like