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!

To get time range values

Hi,
I created a time series scatter plot like example in https://plot.ly/pandas/time-series/.
I want to get time range values ( min and max dates) when I changed time span dynamically on x axis.
Then I am going to use selected time range values to create subset of data frame and map.
I will appreciate if you provide any information
Regards

Hello there,

The min and max values you are looking for are just min(df['Date']) and max(df['Date']). Now with regard to getting this information dynamically, I would actually suggest looking into Dash, another plotly product, to get information about changing a plot dynamically without having to replot.

Let me know if this helps.

P.S. for more reading on comparing date values in python, look here: https://stackoverflow.com/questions/8142364/how-to-compare-two-dates

1 Like

thank you very much indeed.

I had no idea about Dash. By your mail, I looked at it.
It is something like python ploty server. Then I don’t need to use Django for plotty.

Hi,
I looked at Dash.
But I still couldn’t get min and max time values, when I used select time range dynamically.
I used “relayoutData”. But it gives graph coordinates rather than dates.
I think I need to convert axis coords to date values. But I don’t know how.

I will appreciate any help
regards

You can write a function that finds the corresponding date given a leftdate, rightdate and the values that those leftend and rightend dates map to. Something like this:

def get_date_from_axis_coords(leftdate, leftval, rightdate, rightval, value):
    inbetween_dates = []
    delta = rightdate - leftdate
  
    for i in range(delta.days + 1):
        inbetween_dates .append(leftdate + timedelta(days=i))

    # the percentage (out of 1) of value between left value and right value
    value_fraction = (value - leftval) / (rightval - leftval)
    
    return inbetween_dates[ round(value_fraction * len(inbetween_dates))]

In this function you are interpolating between two dates and returning another date by first putting all dates between the leftdate and rightdate into a list and then finding the one that matches to the value you have.

Hope this helps.

1 Like

thank you
but sorry, my confusion still goes on.
How do we get “leftdate” and “rightval” as values in code after zoom/relayout slider.
regards

Now I don’t understand your question. :frowning:
What does after ‘zoom/relayout slider’ mean?

Hi,
my xaxis in time series from 1900 to 2017. when I select a time range, . xaxis changes dynamically by plotly.
I just want to get min and max dates as value in code.
Say, I select time range between 1.1.1980 to 1.1.1985
in python code:

min= ??? # how can I get 1.1.1980 here ?
max=??? #1.1.1985

regards

thank a lot !!!
python Training in Bangalore