Search code examples
pythonplotplotlyzoomingplotly-python

Is there a way to start a plot already zoomed on a specific area using plotly?


I have a scatter plot made with plotly (specifically offline plotly with the Python API on a Jupyter Notebook) and as you know, plotly makes it easy for the user to zoom and frame specific areas, but I'd like the plot to start already focussed on a specific area of my choosing.

I can't find anything relevant in the documentation (maybe because I don't know where to look or what terms to look up). Is there a way to do this, and if so, how? And how does the setting differ when using subplots rather than a Figure object?


Solution

  • When you specify your Layout, under the xaxis and yaxis parameters, you can specify a range, e.g.

    import plotly.graph_objs as go
    
    # ...    
    
    layout = go.Layout(
        yaxis=dict(
            range=[0, 100]
        ),
        xaxis=dict(
            range=[100, 200]
        )
    )
    fig = go.Figure(data=data, layout=layout)
    

    Documentation for this can be found for the xaxis here and yaxis here.