xlim with dates

Hi there, does anyone know if there's a simple way to set an axis limit to a
date? "viewlim_to_dt()" looks promising, but I can't figure out how to use
it...

Thanks a lot,
Alex

···

--
View this message in context: http://old.nabble.com/xlim-with-dates-tp27881612p27881612.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Alex S wrote:

Hi there, does anyone know if there's a simple way to set an axis limit to
a date? "viewlim_to_dt()" looks promising, but I can't figure out how to
use it...

Thanks a lot,
Alex

Alex,

I've done something like this in the past:
            ax.set_xlim( ( min_val, max_val ) )
            # improve formatting
            fig.autofmt_xdate( bottom = 0.20000000000000001, rotation = 30,
ha = 'right' )

            where min_val and max_val are values from
matplotlib.dates.date2num( dt ) because
            dt is a datetime object, which is not the format matplotlib
uses.

So, setting the xlimit ( or ylimit ) using the values expected by
matplotlib manually is one option.

def date2num(d):
    """
    *d* is either a :class:`datetime` instance or a sequence of datetimes.

    Return value is a floating point number (or sequence of floats)
    which gives number of days (fraction part represents hours,
    minutes, seconds) since 0001-01-01 00:00:00 UTC.
    """

-Erik

···

--
View this message in context: http://old.nabble.com/xlim-with-dates-tp27881612p27882067.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Ah perfect, thanks a lot, sorry for the mundane question :slight_smile:

···

--
View this message in context: http://old.nabble.com/xlim-with-dates-tp27881612p27882177.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

If you've plotted using dates, just use dates for the limits:

import matplotlib.pyplot as plt
import numpy as np
from datetime import datetime, timedelta
s = datetime.now()
x = np.array([s, s + timedelta(hours=3)])
y = np.arange(1,3)
plt.plot(x,y)
plt.xlim(s - timedelta(minutes=30), s + timedelta(hours=4))
plt.show()

Ryan

···

On Fri, Mar 12, 2010 at 1:15 PM, Alex S <schmitt.happens@...287...> wrote:

Hi there, does anyone know if there's a simple way to set an axis limit to a
date? "viewlim_to_dt()" looks promising, but I can't figure out how to use
it...

--
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma