1
1
"""
2
2
Matplotlib provides sophisticated date plotting capabilities, standing on the
3
3
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
+ There are a number of helper functions to convert between :mod:`datetime`
16
+ objects and Matplotlib dates:
17
+
18
+ .. currentmodule:: matplotlib.dates
19
+
20
+ .. autosummary::
21
+ :nosignatures:
22
+
23
+ date2num
24
+ num2date
25
+ num2timedelta
26
+ epoch2num
27
+ num2epoch
28
+ mx2num
29
+ drange
9
30
10
31
.. note::
11
32
20
41
732403, whereas using the Gregorian calendar via the datetime
21
42
module we find::
22
43
23
- In [31]: date(2006,4, 1).toordinal() - date(1,1, 1).toordinal()
24
- Out[31]: 732401
44
+ In [1]: date(2006, 4, 1).toordinal() - date(1, 1, 1).toordinal()
45
+ Out[1]: 732401
25
46
47
+ All the Matplotlib date converters, tickers and formatters are timezone aware.
48
+ If no explicit timezone is provided, the rcParam ``timezone`` is assumend. If
49
+ you want to use a custom time zone, pass a :class:`pytz.timezone` instance
50
+ with the tz keyword argument to :func:`num2date`, :func:`.plot_date`, and any
51
+ custom date tickers or locators you create.
52
+ See `pytz <http://pythonhosted.org/pytz/>`_ for information on :mod:`pytz` and
53
+ timezone handling.
26
54
27
55
A wide range of specific and general purpose date tick locators and
28
56
formatters are provided in this module. See
29
57
:mod:`matplotlib.ticker` for general information on tick locators
30
58
and formatters. These are described below.
31
59
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.
41
60
42
61
The `dateutil module <https://dateutil.readthedocs.io/en/stable/>`_ provides
43
62
additional code to handle date ticking, making it easy to place ticks
@@ -350,11 +369,11 @@ def datestr2num(d, default=None):
350
369
351
370
def date2num (d ):
352
371
"""
353
- Converts datetime objects to Matplotlib dates.
372
+ Convert datetime objects to Matplotlib dates.
354
373
355
374
Parameters
356
375
----------
357
- d : :class: `datetime` or sequence of :class:`datetime`
376
+ d : `datetime.datetime ` or `numpy.datetime64` or sequences of these
358
377
359
378
Returns
360
379
-------
@@ -379,11 +398,11 @@ def date2num(d):
379
398
380
399
def julian2num (j ):
381
400
"""
382
- Convert a Julian date (or sequence) to a matplotlib date (or sequence).
401
+ Convert a Julian date (or sequence) to a Matplotlib date (or sequence).
383
402
384
403
Parameters
385
404
----------
386
- k : float or sequence of floats
405
+ j : float or sequence of floats
387
406
Julian date(s)
388
407
389
408
Returns
@@ -417,21 +436,23 @@ def num2julian(n):
417
436
418
437
def num2date (x , tz = None ):
419
438
"""
439
+ Convert Matplotlib dates to `~datetime.datetime` objects.
440
+
420
441
Parameters
421
442
----------
422
443
x : float or sequence of floats
423
444
Number of days (fraction part represents hours, minutes, seconds)
424
445
since 0001-01-01 00:00:00 UTC, plus one.
425
446
tz : string, optional
426
- Timezone of *x* (defaults to rcparams TZ value ).
447
+ Timezone of *x* (defaults to rcparams ``timezone`` ).
427
448
428
449
Returns
429
450
-------
430
- :class:` datetime` or sequence of :class:` datetime`
431
- Dates are returned in timezone *tz*
451
+ `~ datetime.datetime ` or sequence of `~datetime. datetime`
452
+ Dates are returned in timezone *tz*.
432
453
433
- If *x* is a sequence, a sequence of :class:`datetime` objects will
434
- be returned.
454
+ If *x* is a sequence, a sequence of :class:`datetime` objects will
455
+ be returned.
435
456
436
457
Notes
437
458
-----
@@ -459,18 +480,19 @@ def _ordinalf_to_timedelta(x):
459
480
460
481
def num2timedelta (x ):
461
482
"""
462
- Converts number of days to a :class:`timdelta` object.
463
- If *x* is a sequence, a sequence of :class:`timedelta` objects will
483
+ Convert number of days to a `~datetime.timedelta` object.
484
+
485
+ If *x* is a sequence, a sequence of `~datetime.timedelta` objects will
464
486
be returned.
465
487
466
488
Parameters
467
489
----------
468
490
x : float, sequence of floats
469
- Number of days ( fraction part represents hours, minutes, seconds)
491
+ Number of days. The fraction part represents hours, minutes, seconds.
470
492
471
493
Returns
472
494
-------
473
- :class:` timedelta` or list[:class:` timedelta`]
495
+ `datetime. timedelta` or list[`datetime. timedelta`]
474
496
475
497
"""
476
498
if not cbook .iterable (x ):
@@ -484,9 +506,23 @@ def num2timedelta(x):
484
506
485
507
def drange (dstart , dend , delta ):
486
508
"""
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.
509
+ Return a sequence of equally spaced Matplotlib dates.
510
+
511
+ The dates start at *dstart* and reach up to, but not including *dend*.
512
+ They are spaced by *delta*.
513
+
514
+ Parameters
515
+ ----------
516
+ dstart, dend : `~datetime.datetime`
517
+ The date limits.
518
+ delta : `datetime.timedelta`
519
+ Spacing of the dates.
520
+
521
+ Returns
522
+ -------
523
+ drange : `numpy.array`
524
+ A list floats representing Matplotlib dates.
525
+
490
526
"""
491
527
f1 = _to_ordinalf (dstart )
492
528
f2 = _to_ordinalf (dend )
0 commit comments