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

Skip to content

Commit 2076769

Browse files
committed
some cleanup of matplotlib.dates docs
1 parent 3bb328f commit 2076769

1 file changed

Lines changed: 54 additions & 32 deletions

File tree

lib/matplotlib/dates.py

Lines changed: 54 additions & 32 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
@@ -381,12 +387,11 @@ def datestr2num(d, default=None):
381387

382388
def date2num(d):
383389
"""
384-
Converts datetime objects to Matplotlib dates.
390+
Convert datetime objects to Matplotlib dates.
385391
386392
Parameters
387393
----------
388-
d : :class:`datetime` or :class:`numpy.datetime64`, or sequences of
389-
these classes.
394+
d : `datetime.datetime` or `numpy.datetime64` or sequences of these
390395
391396
Returns
392397
-------
@@ -415,11 +420,11 @@ def date2num(d):
415420

416421
def julian2num(j):
417422
"""
418-
Convert a Julian date (or sequence) to a matplotlib date (or sequence).
423+
Convert a Julian date (or sequence) to a Matplotlib date (or sequence).
419424
420425
Parameters
421426
----------
422-
k : float or sequence of floats
427+
j : float or sequence of floats
423428
Julian date(s)
424429
425430
Returns
@@ -453,21 +458,23 @@ def num2julian(n):
453458

454459
def num2date(x, tz=None):
455460
"""
461+
Convert Matplotlib dates to `~datetime.datetime` objects.
462+
456463
Parameters
457464
----------
458465
x : float or sequence of floats
459466
Number of days (fraction part represents hours, minutes, seconds)
460467
since 0001-01-01 00:00:00 UTC, plus one.
461468
tz : string, optional
462-
Timezone of *x* (defaults to rcparams TZ value).
469+
Timezone of *x* (defaults to rcparams ``timezone``).
463470
464471
Returns
465472
-------
466-
:class:`datetime` or sequence of :class:`datetime`
467-
Dates are returned in timezone *tz*
473+
`~datetime.datetime` or sequence of `~datetime.datetime`
474+
Dates are returned in timezone *tz*.
468475
469-
If *x* is a sequence, a sequence of :class:`datetime` objects will
470-
be returned.
476+
If *x* is a sequence, a sequence of :class:`datetime` objects will
477+
be returned.
471478
472479
Notes
473480
-----
@@ -495,18 +502,19 @@ def _ordinalf_to_timedelta(x):
495502

496503
def num2timedelta(x):
497504
"""
498-
Converts number of days to a :class:`timdelta` object.
505+
Convert number of days to a :class:`timdelta` object.
506+
499507
If *x* is a sequence, a sequence of :class:`timedelta` objects will
500508
be returned.
501509
502510
Parameters
503511
----------
504512
x : float, sequence of floats
505-
Number of days (fraction part represents hours, minutes, seconds)
513+
Number of days. The fraction part represents hours, minutes, seconds.
506514
507515
Returns
508516
-------
509-
:class:`timedelta` or list[:class:`timedelta`]
517+
`.timedelta` or list[:class:`.timedelta`]
510518
511519
"""
512520
if not cbook.iterable(x):
@@ -520,9 +528,23 @@ def num2timedelta(x):
520528

521529
def drange(dstart, dend, delta):
522530
"""
523-
Return a date range as float Gregorian ordinals. *dstart* and
524-
*dend* are :class:`datetime` instances. *delta* is a
525-
:class:`datetime.timedelta` instance.
531+
Return a sequence of equally spaced Matplotlib dates.
532+
533+
The dates start at *dstart* and reach up to, but not including *dend*.
534+
They are spaced by *delta*.
535+
536+
Parameters
537+
----------
538+
dstart, dend : `~datetime.datetime`
539+
The date limits.
540+
delta : `datetime.timedelta`
541+
Spacing of the dates.
542+
543+
Returns
544+
-------
545+
drange : `numpy.array`
546+
A list floats representing Matplotlib dates.
547+
526548
"""
527549
f1 = date2num(dstart)
528550
f2 = date2num(dend)

0 commit comments

Comments
 (0)