Search code examples
pythonmatplotlibpython-datetime

Setting datetime axis bounds as half days using Matplotlib


I have the following plot with datetime on the x axis, but I'd like to trim the start and end times to halfway through the days.

plot

The limit setting script is currently

ax1.set_xlim([datetime.date(2020, 9, 16), datetime.date(2020, 9, 26)])

Is there a way to alter the plot to trim to a non full day?


Solution

  • Solved with thanks to Mr T.

    Rather than the previous code, setting the limits in the plotting code, I created an object:

    lims=[np.datetime64('2020-09-16 15:00'), np.datetime64('2020-09-25 17:00')]
    

    Which was then applied to the plotting code:

    ax1.set_xlim(lims)
    

    The resultant plot looks like this:

    Example Plot