Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f5fb78a

Browse files
committed
some cleanup of matplotlib.dates docs
1 parent b503d6a commit f5fb78a

1 file changed

Lines changed: 54 additions & 31 deletions

File tree

lib/matplotlib/dates.py

Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
"""
22
Matplotlib provides sophisticated date plotting capabilities, standing on the
33
shoulders of python :mod:`datetime`, the add-on modules :mod:`pytz` and
4-
:mod:`dateutil`. :class:`datetime` objects are converted to floating point
5-
numbers which represent time in days since 0001-01-01 UTC, plus 1. For
6-
example, 0001-01-01, 06:00 is 1.25, not 0.25. The helper functions
7-
:func:`date2num`, :func:`num2date` and :func:`drange` are used to facilitate
8-
easy conversion to and from :mod:`datetime` and numeric ranges.
4+
:mod:`dateutil`.
5+
6+
7+
.. date-format:
8+
9+
Matplotlib date format
10+
----------------------
11+
Matplotlib represents dates using floating point numbers specifying the number
12+
of days since 0001-01-01 UTC, plus 1. For example, 0001-01-01, 06:00 is 1.25,
13+
not 0.25. Values < 1, i.e. dates before 0001-01-01 UTC are not supported.
14+
15+
The helper functions :func:`date2num`, :func:`num2date` and :func:`drange` are
16+
used for easy conversion between :mod:`datetime` objects and Matplotlib dates.
917
1018
.. note::
1119
@@ -20,24 +28,22 @@
2028
732403, whereas using the Gregorian calendar via the datetime
2129
module we find::
2230
23-
In [31]:date(2006,4,1).toordinal() - date(1,1,1).toordinal()
24-
Out[31]:732401
31+
In [1]: date(2006,4,1).toordinal() - date(1,1,1).toordinal()
32+
Out[1]: 732401
2533
34+
All the Matplotlib date converters, tickers and formatters are timezone aware.
35+
If no explicit timezone is provided, the rcParam ``timezone`` is assumend. If
36+
you want to use a custom time zone, pass a :class:`pytz.timezone` instance
37+
with the tz keyword argument to :func:`num2date`, :func:`.plot_date`, and any
38+
custom date tickers or locators you create.
39+
See `pytz <http://pythonhosted.org/pytz/>`_ for information on :mod:`pytz` and
40+
timezone handling.
2641
2742
A wide range of specific and general purpose date tick locators and
2843
formatters are provided in this module. See
2944
:mod:`matplotlib.ticker` for general information on tick locators
3045
and formatters. These are described below.
3146
32-
All the matplotlib date converters, tickers and formatters are
33-
timezone aware, and the default timezone is given by the timezone
34-
parameter in your :file:`matplotlibrc` file. If you leave out a
35-
:class:`tz` timezone instance, the default from your rc file will be
36-
assumed. If you want to use a custom time zone, pass a
37-
:class:`pytz.timezone` instance with the tz keyword argument to
38-
:func:`num2date`, :func:`plot_date`, and any custom date tickers or
39-
locators you create. See `pytz <http://pythonhosted.org/pytz/>`_ for
40-
information on :mod:`pytz` and timezone handling.
4147
4248
The `dateutil module <https://dateutil.readthedocs.io/en/stable/>`_ provides
4349
additional code to handle date ticking, making it easy to place ticks
@@ -350,11 +356,11 @@ def datestr2num(d, default=None):
350356

351357
def date2num(d):
352358
"""
353-
Converts datetime objects to Matplotlib dates.
359+
Convert datetime objects to Matplotlib dates.
354360
355361
Parameters
356362
----------
357-
d : :class:`datetime` or sequence of :class:`datetime`
363+
d : `datetime.datetime` or `numpy.datetime64` or sequences of these
358364
359365
Returns
360366
-------
@@ -379,11 +385,11 @@ def date2num(d):
379385

380386
def julian2num(j):
381387
"""
382-
Convert a Julian date (or sequence) to a matplotlib date (or sequence).
388+
Convert a Julian date (or sequence) to a Matplotlib date (or sequence).
383389
384390
Parameters
385391
----------
386-
k : float or sequence of floats
392+
j : float or sequence of floats
387393
Julian date(s)
388394
389395
Returns
@@ -417,21 +423,23 @@ def num2julian(n):
417423

418424
def num2date(x, tz=None):
419425
"""
426+
Convert Matplotlib dates to `~datetime.datetime` objects.
427+
420428
Parameters
421429
----------
422430
x : float or sequence of floats
423431
Number of days (fraction part represents hours, minutes, seconds)
424432
since 0001-01-01 00:00:00 UTC, plus one.
425433
tz : string, optional
426-
Timezone of *x* (defaults to rcparams TZ value).
434+
Timezone of *x* (defaults to rcparams ``timezone``).
427435
428436
Returns
429437
-------
430-
:class:`datetime` or sequence of :class:`datetime`
431-
Dates are returned in timezone *tz*
438+
`~datetime.datetime` or sequence of `~datetime.datetime`
439+
Dates are returned in timezone *tz*.
432440
433-
If *x* is a sequence, a sequence of :class:`datetime` objects will
434-
be returned.
441+
If *x* is a sequence, a sequence of :class:`datetime` objects will
442+
be returned.
435443
436444
Notes
437445
-----
@@ -459,18 +467,19 @@ def _ordinalf_to_timedelta(x):
459467

460468
def num2timedelta(x):
461469
"""
462-
Converts number of days to a :class:`timdelta` object.
470+
Convert number of days to a :class:`timdelta` object.
471+
463472
If *x* is a sequence, a sequence of :class:`timedelta` objects will
464473
be returned.
465474
466475
Parameters
467476
----------
468477
x : float, sequence of floats
469-
Number of days (fraction part represents hours, minutes, seconds)
478+
Number of days. The fraction part represents hours, minutes, seconds.
470479
471480
Returns
472481
-------
473-
:class:`timedelta` or list[:class:`timedelta`]
482+
`.timedelta` or list[:class:`.timedelta`]
474483
475484
"""
476485
if not cbook.iterable(x):
@@ -484,9 +493,23 @@ def num2timedelta(x):
484493

485494
def drange(dstart, dend, delta):
486495
"""
487-
Return a date range as float Gregorian ordinals. *dstart* and
488-
*dend* are :class:`datetime` instances. *delta* is a
489-
:class:`datetime.timedelta` instance.
496+
Return a sequence of equally spaced Matplotlib dates.
497+
498+
The dates start at *dstart* and reach up to, but not including *dend*.
499+
They are spaced by *delta*.
500+
501+
Parameters
502+
----------
503+
dstart, dend : `~datetime.datetime`
504+
The date limits.
505+
delta : `datetime.timedelta`
506+
Spacing of the dates.
507+
508+
Returns
509+
-------
510+
drange : `numpy.array`
511+
A list floats representing Matplotlib dates.
512+
490513
"""
491514
f1 = _to_ordinalf(dstart)
492515
f2 = _to_ordinalf(dend)

0 commit comments

Comments
 (0)