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

Skip to content

Commit 0b05b77

Browse files
committed
ENH: add rcParam for ConciseDate and interval_multiples
1 parent ef9fc20 commit 0b05b77

File tree

3 files changed

+87
-10
lines changed

3 files changed

+87
-10
lines changed

lib/matplotlib/dates.py

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,8 +1784,11 @@ class DateConverter(units.ConversionInterface):
17841784
The 'unit' tag for such data is None or a tzinfo instance.
17851785
"""
17861786

1787-
@staticmethod
1788-
def axisinfo(unit, axis):
1787+
def __init__(self, interval_multiples=True):
1788+
self._interval_multiples = interval_multiples
1789+
super().__init__()
1790+
1791+
def axisinfo(self, unit, axis):
17891792
"""
17901793
Return the `~matplotlib.units.AxisInfo` for *unit*.
17911794
@@ -1794,7 +1797,8 @@ def axisinfo(unit, axis):
17941797
"""
17951798
tz = unit
17961799

1797-
majloc = AutoDateLocator(tz=tz)
1800+
majloc = AutoDateLocator(tz=tz,
1801+
interval_multiples=self._interval_multiples)
17981802
majfmt = AutoDateFormatter(majloc, tz=tz)
17991803
datemin = datetime.date(2000, 1, 1)
18001804
datemax = datetime.date(2010, 1, 1)
@@ -1836,17 +1840,19 @@ class ConciseDateConverter(DateConverter):
18361840
# docstring inherited
18371841

18381842
def __init__(self, formats=None, zero_formats=None, offset_formats=None,
1839-
show_offset=True):
1843+
show_offset=True, interval_multiples=True):
18401844
self._formats = formats
18411845
self._zero_formats = zero_formats
18421846
self._offset_formats = offset_formats
18431847
self._show_offset = show_offset
1848+
self._interval_multiples = interval_multiples
18441849
super().__init__()
18451850

18461851
def axisinfo(self, unit, axis):
18471852
# docstring inherited
18481853
tz = unit
1849-
majloc = AutoDateLocator(tz=tz)
1854+
majloc = AutoDateLocator(tz=tz,
1855+
interval_multiples=self._interval_multiples)
18501856
majfmt = ConciseDateFormatter(majloc, tz=tz, formats=self._formats,
18511857
zero_formats=self._zero_formats,
18521858
offset_formats=self._offset_formats,
@@ -1857,6 +1863,49 @@ def axisinfo(self, unit, axis):
18571863
default_limits=(datemin, datemax))
18581864

18591865

1860-
units.registry[np.datetime64] = DateConverter()
1861-
units.registry[datetime.date] = DateConverter()
1862-
units.registry[datetime.datetime] = DateConverter()
1866+
# The following is so that we can set the converter for dates
1867+
# via the validator for the rcParams `date.converter` and
1868+
# `date.interval_multiples`
1869+
1870+
1871+
global _converter
1872+
_conv_st = 'auto'
1873+
global _int_mult
1874+
_int_mult = True
1875+
1876+
1877+
def _set_converter(s):
1878+
"""Called by validator for rcParams date.converter"""
1879+
global _conv_st
1880+
_conv_st = s
1881+
_register_converters()
1882+
1883+
1884+
def _set_int_mult(b):
1885+
"""Called by validator for rcParams date.interval_multiples"""
1886+
global _int_mult
1887+
_int_mult = b
1888+
_register_converters()
1889+
1890+
1891+
def _register_converters():
1892+
"""
1893+
Helper to register the date converters when rcParams `date.converter` and
1894+
`date.interval_multiples` are changed. Called by the helpers above.
1895+
"""
1896+
global _conv_st
1897+
global _int_mult
1898+
if _conv_st == 'concise':
1899+
converter = ConciseDateConverter
1900+
else:
1901+
converter = DateConverter
1902+
1903+
interval_multiples = _int_mult
1904+
units.registry[np.datetime64] = converter(
1905+
interval_multiples=interval_multiples)
1906+
units.registry[datetime.date] = converter(
1907+
interval_multiples=interval_multiples)
1908+
units.registry[datetime.datetime] = converter(
1909+
interval_multiples=interval_multiples)
1910+
1911+
# _register_converters()

lib/matplotlib/rcsetup.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,26 @@ def validate_bool_maybe_none(b):
154154
raise ValueError('Could not convert "%s" to bool' % b)
155155

156156

157+
def _validate_date_converter(s):
158+
s = validate_string(s)
159+
try:
160+
import matplotlib.dates as mdates
161+
mdates._set_converter(s)
162+
except Exception as e:
163+
pass
164+
165+
166+
def _validate_date_int_mult(s):
167+
if s is None:
168+
return
169+
s = validate_bool(s)
170+
try:
171+
import matplotlib.dates as mdates
172+
mdates._set_int_mult(s)
173+
except Exception as e:
174+
pass
175+
176+
157177
def _validate_tex_preamble(s):
158178
message = (
159179
f"Support for setting the 'text.latex.preamble' and 'pgf.preamble' "
@@ -1266,6 +1286,11 @@ def _convert_validator_spec(key, conv):
12661286
'date.autoformatter.second': ['%H:%M:%S', validate_string],
12671287
'date.autoformatter.microsecond': ['%M:%S.%f', validate_string],
12681288

1289+
# 'auto', 'concise', 'auto-noninterval'
1290+
'date.converter': ['auto', _validate_date_converter],
1291+
# for auto date locator, choose interval_multiples
1292+
'date.interval_multiples': [True, _validate_date_int_mult],
1293+
12691294
#legend properties
12701295
'legend.fancybox': [True, validate_bool],
12711296
'legend.loc': ['best',

matplotlibrc.template

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@
326326
#mathtext.sf: sans
327327
#mathtext.tt: monospace
328328
#mathtext.fallback: cm # Select fallback font from ['cm' (Computer Modern), 'stix'
329-
# 'stixsans'] when a symbol can not be found in one of the
329+
# 'stixsans'] when a symbol can not be found in one of the
330330
# custom math fonts. Select 'None' to not perform fallback
331331
# and replace the missing character by a dummy symbol.
332332
#mathtext.default: it # The default font to use for math.
@@ -431,7 +431,10 @@
431431
#date.autoformatter.minute: %d %H:%M
432432
#date.autoformatter.second: %H:%M:%S
433433
#date.autoformatter.microsecond: %M:%S.%f
434-
434+
## 'auto', 'concise':
435+
#date.converter: 'auto'
436+
## For auto converter whether to use interval_multiples:
437+
#date.interval_multiples: True
435438

436439
## ***************************************************************************
437440
## * TICKS *

0 commit comments

Comments
 (0)