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

Skip to content

Commit 1dfa196

Browse files
authored
Merge pull request #10246 from timhoffm/axes-doc-plot_date
improve docstring of Axes.plot_date
2 parents 56976cf + 9a3f04a commit 1dfa196

File tree

2 files changed

+106
-69
lines changed

2 files changed

+106
-69
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,64 +1535,66 @@ def plot(self, *args, **kwargs):
15351535
def plot_date(self, x, y, fmt='o', tz=None, xdate=True, ydate=False,
15361536
**kwargs):
15371537
"""
1538-
A plot with data that contains dates.
1539-
1540-
Similar to the :func:`~matplotlib.pyplot.plot` command, except
1541-
the *x* or *y* (or both) data is considered to be dates, and the
1542-
axis is labeled accordingly.
1543-
1544-
*x* and/or *y* can be a sequence of dates represented as float
1545-
days since 0001-01-01 UTC.
1546-
1547-
Note if you are using custom date tickers and formatters, it
1548-
may be necessary to set the formatters/locators after the call
1549-
to :meth:`plot_date` since :meth:`plot_date` will set the
1550-
default tick locator to
1551-
:class:`matplotlib.dates.AutoDateLocator` (if the tick
1552-
locator is not already set to a
1553-
:class:`matplotlib.dates.DateLocator` instance) and the
1554-
default tick formatter to
1555-
:class:`matplotlib.dates.AutoDateFormatter` (if the tick
1556-
formatter is not already set to a
1557-
:class:`matplotlib.dates.DateFormatter` instance).
1538+
Plot data that contains dates.
15581539
1540+
Similar to `.plot`, this plots *y* vs. *x* as lines or markers.
1541+
However, the axis labels are formatted as dates depending on *xdate*
1542+
and *ydate*.
15591543
15601544
Parameters
15611545
----------
1562-
fmt : string
1563-
The plot format string.
1546+
x, y : array-like
1547+
The coordinates of the data points. If *xdate* or *ydate* is
1548+
*True*, the respective values *x* or *y* are interpreted as
1549+
:ref:`Matplotlib dates <date-format>`.
1550+
1551+
fmt : str, optional
1552+
The plot format string. For details, see the corresponding
1553+
parameter in `.plot`.
15641554
15651555
tz : [ *None* | timezone string | :class:`tzinfo` instance]
1566-
The time zone to use in labeling dates. If *None*, defaults to rc
1567-
value.
1556+
The time zone to use in labeling dates. If *None*, defaults to
1557+
rcParam ``timezone``.
15681558
1569-
xdate : boolean
1570-
If *True*, the *x*-axis will be labeled with dates.
1559+
xdate : bool, optional, default: True
1560+
If *True*, the *x*-axis will be interpreted as Matplotlib dates.
15711561
1572-
ydate : boolean
1573-
If *True*, the *y*-axis will be labeled with dates.
1562+
ydate : bool, optional, default: False
1563+
If *True*, the *y*-axis will be interpreted as Matplotlib dates.
15741564
15751565
15761566
Returns
15771567
-------
15781568
lines
1569+
A list of `.Line2D` objects that were added to the axes.
15791570
15801571
1581-
See Also
1582-
--------
1583-
matplotlib.dates : helper functions on dates
1584-
matplotlib.dates.date2num : how to convert dates to num
1585-
matplotlib.dates.num2date : how to convert num to dates
1586-
matplotlib.dates.drange : how floating point dates
1587-
15881572
Other Parameters
15891573
----------------
1590-
**kwargs :
1574+
**kwargs
15911575
Keyword arguments control the :class:`~matplotlib.lines.Line2D`
15921576
properties:
15931577
15941578
%(Line2D)s
15951579
1580+
1581+
See Also
1582+
--------
1583+
matplotlib.dates : Helper functions on dates.
1584+
matplotlib.dates.date2num : Convert dates to num.
1585+
matplotlib.dates.num2date : Convert num to dates.
1586+
matplotlib.dates.drange : Create an equally spaced sequence of dates.
1587+
1588+
1589+
Notes
1590+
-----
1591+
If you are using custom date tickers and formatters, it may be
1592+
necessary to set the formatters/locators after the call to
1593+
`.plot_date`. `.plot_date` will set the default tick locator to
1594+
`.AutoDateLocator` (if the tick locator is not already set to a
1595+
`.DateLocator` instance) and the default tick formatter to
1596+
`.AutoDateFormatter` (if the tick formatter is not already set to a
1597+
`.DateFormatter` instance).
15961598
"""
15971599

15981600
if not self._hold:

lib/matplotlib/dates.py

Lines changed: 68 additions & 33 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
@@ -381,12 +400,11 @@ def datestr2num(d, default=None):
381400

382401
def date2num(d):
383402
"""
384-
Converts datetime objects to Matplotlib dates.
403+
Convert datetime objects to Matplotlib dates.
385404
386405
Parameters
387406
----------
388-
d : :class:`datetime` or :class:`numpy.datetime64`, or sequences of
389-
these classes.
407+
d : `datetime.datetime` or `numpy.datetime64` or sequences of these
390408
391409
Returns
392410
-------
@@ -415,11 +433,11 @@ def date2num(d):
415433

416434
def julian2num(j):
417435
"""
418-
Convert a Julian date (or sequence) to a matplotlib date (or sequence).
436+
Convert a Julian date (or sequence) to a Matplotlib date (or sequence).
419437
420438
Parameters
421439
----------
422-
k : float or sequence of floats
440+
j : float or sequence of floats
423441
Julian date(s)
424442
425443
Returns
@@ -453,21 +471,23 @@ def num2julian(n):
453471

454472
def num2date(x, tz=None):
455473
"""
474+
Convert Matplotlib dates to `~datetime.datetime` objects.
475+
456476
Parameters
457477
----------
458478
x : float or sequence of floats
459479
Number of days (fraction part represents hours, minutes, seconds)
460480
since 0001-01-01 00:00:00 UTC, plus one.
461481
tz : string, optional
462-
Timezone of *x* (defaults to rcparams TZ value).
482+
Timezone of *x* (defaults to rcparams ``timezone``).
463483
464484
Returns
465485
-------
466-
:class:`datetime` or sequence of :class:`datetime`
467-
Dates are returned in timezone *tz*
486+
`~datetime.datetime` or sequence of `~datetime.datetime`
487+
Dates are returned in timezone *tz*.
468488
469-
If *x* is a sequence, a sequence of :class:`datetime` objects will
470-
be returned.
489+
If *x* is a sequence, a sequence of :class:`datetime` objects will
490+
be returned.
471491
472492
Notes
473493
-----
@@ -495,18 +515,19 @@ def _ordinalf_to_timedelta(x):
495515

496516
def num2timedelta(x):
497517
"""
498-
Converts number of days to a :class:`timdelta` object.
499-
If *x* is a sequence, a sequence of :class:`timedelta` objects will
518+
Convert number of days to a `~datetime.timedelta` object.
519+
520+
If *x* is a sequence, a sequence of `~datetime.timedelta` objects will
500521
be returned.
501522
502523
Parameters
503524
----------
504525
x : float, sequence of floats
505-
Number of days (fraction part represents hours, minutes, seconds)
526+
Number of days. The fraction part represents hours, minutes, seconds.
506527
507528
Returns
508529
-------
509-
:class:`timedelta` or list[:class:`timedelta`]
530+
`datetime.timedelta` or list[`datetime.timedelta`]
510531
511532
"""
512533
if not cbook.iterable(x):
@@ -520,9 +541,23 @@ def num2timedelta(x):
520541

521542
def drange(dstart, dend, delta):
522543
"""
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.
544+
Return a sequence of equally spaced Matplotlib dates.
545+
546+
The dates start at *dstart* and reach up to, but not including *dend*.
547+
They are spaced by *delta*.
548+
549+
Parameters
550+
----------
551+
dstart, dend : `~datetime.datetime`
552+
The date limits.
553+
delta : `datetime.timedelta`
554+
Spacing of the dates.
555+
556+
Returns
557+
-------
558+
drange : `numpy.array`
559+
A list floats representing Matplotlib dates.
560+
526561
"""
527562
f1 = date2num(dstart)
528563
f2 = date2num(dend)

0 commit comments

Comments
 (0)