Autorange for yaxis based on min and max values within visible xaxis range

Hello,

I am very new at Plotly and I am trying to get Plotly to ignore shapes I added (green/orange/red squares to illustrate the severity) when drawing the values. Indeed, sometimes values oscillate within a very narrow field, and I do not want to see the whole shapes, it would make the value seem flat.

To do this, I assume I need to calculate the min and max value within the visible xaxis range, so that if I scroll it adapts the y range.

If this is not possible, my plan B would be “min/max within the last X days”.

Any help is highly appreciated.

Hi @xus72 ,

Welcome to the forum!

If I understand you correctly, you can define min/max x axis range and y axis will adapt to current display, by selecting your range interactively like image below .

zoom_plot1

Hope this help.

Exactly, except I have shapes additionally, which should be ignored. That what I want to define the yaxis range manually.

I just don’t know how to express “min value within visible range”…

Hi @xus72 ,

I think you can get your plot visible range both x and y axis by using min and max function.

For example you have a data frame as df that have column x and y to make a scatter plot.
To get range of y you can use df["y"].min() for min range , and use df["x"].max() for max range.
For x axis range you can use df["x"].min() and df["y"].max().

After that you can set range using fig.update_yaxes and fig.update_xaxes.

Let’s say, I create Green Shape and some Tiny Red Scatter plot before set range manually will be displayed below.

After I set The Range Manually based on visible range of the Tiny Red Scatter plot.

The code below is implementation after set y and x range manually.

import plotly.graph_objects as go
import pandas as pd 

df = pd.DataFrame(dict(
	x = [2.1,2.15,2.2,2.25,2.3,2.35,2.4,2.45,2.5,2.55],
	y = [2.1,2.120,2.11,2.105,2.10,2.095,2.0899,2.08,2.105,2.07]
))

fig = go.Figure()

fig.add_trace(go.Scatter(
    x=df["x"],
    y=df["y"],
    line=dict(color="red")
))


# Add shapes
fig.add_shape(type="rect",
    x0=1, y0=1.5, x1=4, y1=2.5,
    line=dict(
        color="Green",
        width=4,
    ),
    fillcolor="SeaGreen",
    layer="below"
)
# calculate min and max y
y_padding = (df["y"].max()-df["y"].min())/len(df["y"])
min_y = df["y"].min() - y_padding
max_y = df["y"].max() + y_padding


# calculate min and max x
x_padding = (df["x"].max()-df["x"].min())/len(df["x"])
min_x = df["x"].min()-x_padding
max_x = df["x"].max()+x_padding


# Set y and x range manually
## You can comment this line to see result before set y range
fig.update_yaxes(range=[min_y,max_y])

## You can comment this line to see result before set x range
fig.update_xaxes(range=[min_x, max_x])

fig.show()

Hope this help.

Many thanks, this is already very helpful. I like he padding a lot, very nice way of adding a proportional padding.

What I am after is having min_y and max_y defined depending on the currently visible x range.

Let’s say I zoom in and only see the range 2.2<x<2.4 on the graph.

I would like y range to recalculate automatically based on 2.09<y<2.11 (whereas for the entire series we have 2.07<y<2.12)

Sorry if my explanations are not very clear, I am very new at plotly, and thanks a lot for helping out.

Hi @xus72 ,

I think you can do with selecting the data frame itself (if you are using pandas data frame).

for example if you want select x data between 2.2<x<2.4 and the y data will be following based on selected x.

And, if you want to filter again based on y , you can select on filtered data frame before.

...

# filtered dataframe based on 2.2<x<2.4
df = df .loc[(df["x"]> 2.2) & (df["x"]< 2.4)

# filtered dataframe based on 2.09<y<2.11
df = df .loc[(df["y"]> 2.09) & (df["y"]< 2.11)

....

Glad to help, :slightly_smiling_face:

cool, thanks, so there’s no way to do express “min x currently visible” and “max x currently visible”?

I see, you want functionality like in Axes.get_xlim() in matplotlib, right ? .

As far as I concern, those methods don’t available for now in plotly.

So you have to do manually by estimating range use x and y data.

But, I think another member in this forum will give their thought about your problem you are facing now.

Cheers.