Converter for `datetime.date` and `datetime.datetime` data, or for date/time data represented as it would be converted by `date2num`. The 'unit' tag for such data is None or a `~datetime.tzinfo` instance.
| 1726 | |
| 1727 | |
| 1728 | class DateConverter(units.ConversionInterface): |
| 1729 | """ |
| 1730 | Converter for `datetime.date` and `datetime.datetime` data, or for |
| 1731 | date/time data represented as it would be converted by `date2num`. |
| 1732 | |
| 1733 | The 'unit' tag for such data is None or a `~datetime.tzinfo` instance. |
| 1734 | """ |
| 1735 | |
| 1736 | def __init__(self, *, interval_multiples=True): |
| 1737 | self._interval_multiples = interval_multiples |
| 1738 | super().__init__() |
| 1739 | |
| 1740 | def axisinfo(self, unit, axis): |
| 1741 | """ |
| 1742 | Return the `~matplotlib.units.AxisInfo` for *unit*. |
| 1743 | |
| 1744 | *unit* is a `~datetime.tzinfo` instance or None. |
| 1745 | The *axis* argument is required but not used. |
| 1746 | """ |
| 1747 | tz = unit |
| 1748 | |
| 1749 | majloc = AutoDateLocator(tz=tz, |
| 1750 | interval_multiples=self._interval_multiples) |
| 1751 | majfmt = AutoDateFormatter(majloc, tz=tz) |
| 1752 | datemin = datetime.date(1970, 1, 1) |
| 1753 | datemax = datetime.date(1970, 1, 2) |
| 1754 | |
| 1755 | return units.AxisInfo(majloc=majloc, majfmt=majfmt, label='', |
| 1756 | default_limits=(datemin, datemax)) |
| 1757 | |
| 1758 | @staticmethod |
| 1759 | def convert(value, unit, axis): |
| 1760 | """ |
| 1761 | If *value* is not already a number or sequence of numbers, convert it |
| 1762 | with `date2num`. |
| 1763 | |
| 1764 | The *unit* and *axis* arguments are not used. |
| 1765 | """ |
| 1766 | return date2num(value) |
| 1767 | |
| 1768 | @staticmethod |
| 1769 | def default_units(x, axis): |
| 1770 | """ |
| 1771 | Return the `~datetime.tzinfo` instance of *x* or of its first element, |
| 1772 | or None |
| 1773 | """ |
| 1774 | if isinstance(x, np.ndarray): |
| 1775 | x = x.ravel() |
| 1776 | |
| 1777 | try: |
| 1778 | x = cbook._safe_first_finite(x) |
| 1779 | except (TypeError, StopIteration): |
| 1780 | pass |
| 1781 | |
| 1782 | try: |
| 1783 | return x.tzinfo |
| 1784 | except AttributeError: |
| 1785 | pass |
no outgoing calls
searching dependent graphs…