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

Skip to content

Commit 6d9eab7

Browse files
jklymaktacaswell
authored andcommitted
Merge pull request #10246 from timhoffm/axes-doc-plot_date
improve docstring of Axes.plot_date Conflicts: lib/matplotlib/dates.py - both branches changed docstring, kept master version
1 parent 01f95b1 commit 6d9eab7

File tree

2 files changed

+106
-68
lines changed

2 files changed

+106
-68
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,64 +1381,66 @@ def plot(self, *args, **kwargs):
13811381
def plot_date(self, x, y, fmt='o', tz=None, xdate=True, ydate=False,
13821382
**kwargs):
13831383
"""
1384-
A plot with data that contains dates.
1385-
1386-
Similar to the :func:`~matplotlib.pyplot.plot` command, except
1387-
the *x* or *y* (or both) data is considered to be dates, and the
1388-
axis is labeled accordingly.
1389-
1390-
*x* and/or *y* can be a sequence of dates represented as float
1391-
days since 0001-01-01 UTC.
1392-
1393-
Note if you are using custom date tickers and formatters, it
1394-
may be necessary to set the formatters/locators after the call
1395-
to :meth:`plot_date` since :meth:`plot_date` will set the
1396-
default tick locator to
1397-
:class:`matplotlib.dates.AutoDateLocator` (if the tick
1398-
locator is not already set to a
1399-
:class:`matplotlib.dates.DateLocator` instance) and the
1400-
default tick formatter to
1401-
:class:`matplotlib.dates.AutoDateFormatter` (if the tick
1402-
formatter is not already set to a
1403-
:class:`matplotlib.dates.DateFormatter` instance).
1384+
Plot data that contains dates.
14041385
1386+
Similar to `.plot`, this plots *y* vs. *x* as lines or markers.
1387+
However, the axis labels are formatted as dates depending on *xdate*
1388+
and *ydate*.
14051389
14061390
Parameters
14071391
----------
1408-
fmt : string
1409-
The plot format string.
1392+
x, y : array-like
1393+
The coordinates of the data points. If *xdate* or *ydate* is
1394+
*True*, the respective values *x* or *y* are interpreted as
1395+
:ref:`Matplotlib dates <date-format>`.
1396+
1397+
fmt : str, optional
1398+
The plot format string. For details, see the corresponding
1399+
parameter in `.plot`.
14101400
14111401
tz : [ *None* | timezone string | :class:`tzinfo` instance]
1412-
The time zone to use in labeling dates. If *None*, defaults to rc
1413-
value.
1402+
The time zone to use in labeling dates. If *None*, defaults to
1403+
rcParam ``timezone``.
14141404
1415-
xdate : boolean
1416-
If *True*, the *x*-axis will be labeled with dates.
1405+
xdate : bool, optional, default: True
1406+
If *True*, the *x*-axis will be interpreted as Matplotlib dates.
14171407
1418-
ydate : boolean
1419-
If *True*, the *y*-axis will be labeled with dates.
1408+
ydate : bool, optional, default: False
1409+
If *True*, the *y*-axis will be interpreted as Matplotlib dates.
14201410
14211411
14221412
Returns
14231413
-------
14241414
lines
1415+
A list of `.Line2D` objects that were added to the axes.
14251416
14261417
1427-
See Also
1428-
--------
1429-
matplotlib.dates : helper functions on dates
1430-
matplotlib.dates.date2num : how to convert dates to num
1431-
matplotlib.dates.num2date : how to convert num to dates
1432-
matplotlib.dates.drange : how floating point dates
1433-
14341418
Other Parameters
14351419
----------------
1436-
**kwargs :
1420+
**kwargs
14371421
Keyword arguments control the :class:`~matplotlib.lines.Line2D`
14381422
properties:
14391423
14401424
%(Line2D)s
14411425
1426+
1427+
See Also
1428+
--------
1429+
matplotlib.dates : Helper functions on dates.
1430+
matplotlib.dates.date2num : Convert dates to num.
1431+
matplotlib.dates.num2date : Convert num to dates.
1432+
matplotlib.dates.drange : Create an equally spaced sequence of dates.
1433+
1434+
1435+
Notes
1436+
-----
1437+
If you are using custom date tickers and formatters, it may be
1438+
necessary to set the formatters/locators after the call to
1439+
`.plot_date`. `.plot_date` will set the default tick locator to
1440+
`.AutoDateLocator` (if the tick locator is not already set to a
1441+
`.DateLocator` instance) and the default tick formatter to
1442+
`.AutoDateFormatter` (if the tick formatter is not already set to a
1443+
`.DateFormatter` instance).
14421444
"""
14431445

14441446
if not self._hold:

lib/matplotlib/dates.py

Lines changed: 68 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
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+
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
930
1031
.. note::
1132
@@ -20,24 +41,22 @@
2041
732403, whereas using the Gregorian calendar via the datetime
2142
module we find::
2243
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
2546
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.
2654
2755
A wide range of specific and general purpose date tick locators and
2856
formatters are provided in this module. See
2957
:mod:`matplotlib.ticker` for general information on tick locators
3058
and formatters. These are described below.
3159
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.
4160
4261
The `dateutil module <https://dateutil.readthedocs.io/en/stable/>`_ provides
4362
additional code to handle date ticking, making it easy to place ticks
@@ -350,11 +369,11 @@ def datestr2num(d, default=None):
350369

351370
def date2num(d):
352371
"""
353-
Converts datetime objects to Matplotlib dates.
372+
Convert datetime objects to Matplotlib dates.
354373
355374
Parameters
356375
----------
357-
d : :class:`datetime` or sequence of :class:`datetime`
376+
d : `datetime.datetime` or `numpy.datetime64` or sequences of these
358377
359378
Returns
360379
-------
@@ -379,11 +398,11 @@ def date2num(d):
379398

380399
def julian2num(j):
381400
"""
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).
383402
384403
Parameters
385404
----------
386-
k : float or sequence of floats
405+
j : float or sequence of floats
387406
Julian date(s)
388407
389408
Returns
@@ -417,21 +436,23 @@ def num2julian(n):
417436

418437
def num2date(x, tz=None):
419438
"""
439+
Convert Matplotlib dates to `~datetime.datetime` objects.
440+
420441
Parameters
421442
----------
422443
x : float or sequence of floats
423444
Number of days (fraction part represents hours, minutes, seconds)
424445
since 0001-01-01 00:00:00 UTC, plus one.
425446
tz : string, optional
426-
Timezone of *x* (defaults to rcparams TZ value).
447+
Timezone of *x* (defaults to rcparams ``timezone``).
427448
428449
Returns
429450
-------
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*.
432453
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.
435456
436457
Notes
437458
-----
@@ -459,18 +480,19 @@ def _ordinalf_to_timedelta(x):
459480

460481
def num2timedelta(x):
461482
"""
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
464486
be returned.
465487
466488
Parameters
467489
----------
468490
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.
470492
471493
Returns
472494
-------
473-
:class:`timedelta` or list[:class:`timedelta`]
495+
`datetime.timedelta` or list[`datetime.timedelta`]
474496
475497
"""
476498
if not cbook.iterable(x):
@@ -484,9 +506,23 @@ def num2timedelta(x):
484506

485507
def drange(dstart, dend, delta):
486508
"""
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+
490526
"""
491527
f1 = _to_ordinalf(dstart)
492528
f2 = _to_ordinalf(dend)

0 commit comments

Comments
 (0)