Xlim for datetime

Plotting a pd.Series with a DatetimeIndex, I am wondering how one can constrain the x-axis to, e.g. the first 15 minutes?

You can constrain the data range at any level:

  • dataframe level: You can use Pandas filtering on that column to restrict the data before plotting (e.g. df.loc['2021-01-01':'2021-01-02'])
  • hvPlot call level: Add an xlim=(...,...) argument to the hvPlot call
  • HoloViews Element level: Once you get the output of .hvplot(), you can apply
    .redim.range(x=(pd.DateTime(...), pd....) to constrain the declared dimension bounds
  • Plot level: You can declare an xlim=(pd.DateTime(...), pd...) that applies to the actual plot

I think it’s up to you to decide what level makes the most sense.

1 Like

Strongly recommend using either the DataFrame or xlim option if you’re working with hvPlot, always try to minimize the use of HoloViews API where you can. Also strongly recommend using tuples for xlim although a list will probably also work (edited @jbednar’s reply to reflect that).

1 Like

what I was missing is that I need to have a pd.DateTime(tstring) as the input to xlim!
Thanks for your replies!

out of learning interest though: What is the reasoning behind avoiding HoloviewsAPI?

If you’re familiar with it and don’t think you’ll be sharing the code I don’t think there’s a strong imperative to avoid it. The main problem is the depth of knowledge required to understand it. hvPlot has a much shallower learning curve than HoloViews so for someone knew coming to your code it’ll be much more transparent what’s going on than if you start diving into HoloViews. So as long as hvPlot provides the features you need it’s going to be simpler than mix- and matching which APIs you’re using.

1 Like