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

Skip to content

Commit 54460f5

Browse files
committed
TST: add a test
1 parent 9d686a5 commit 54460f5

File tree

3 files changed

+77
-40
lines changed

3 files changed

+77
-40
lines changed

lib/matplotlib/dates.py

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,45 +1915,42 @@ def axisinfo(self, unit, axis):
19151915
# via the validator for the rcParams `date.converter` and
19161916
# `date.interval_multiples`
19171917

1918-
1919-
global _converter
1920-
_conv_st = 'auto'
1921-
global _int_mult
1922-
_int_mult = True
1923-
1924-
1925-
def _set_converter(s):
1926-
"""Called by validator for rcParams date.converter"""
1927-
global _conv_st
1928-
_conv_st = s
1929-
_register_converters()
1930-
1931-
1932-
def _set_int_mult(b):
1933-
"""Called by validator for rcParams date.interval_multiples"""
1934-
global _int_mult
1935-
_int_mult = b
1936-
_register_converters()
1937-
1938-
1939-
def _register_converters():
1918+
class _rcParam_helper:
19401919
"""
1941-
Helper to register the date converters when rcParams `date.converter` and
1942-
`date.interval_multiples` are changed. Called by the helpers above.
1920+
Never instatiated, but manages the rcParams for dates...
19431921
"""
1944-
global _conv_st
1945-
global _int_mult
1946-
if _conv_st == 'concise':
1947-
converter = ConciseDateConverter
1948-
else:
1949-
converter = DateConverter
19501922

1951-
interval_multiples = _int_mult
1952-
units.registry[np.datetime64] = converter(
1953-
interval_multiples=interval_multiples)
1954-
units.registry[datetime.date] = converter(
1955-
interval_multiples=interval_multiples)
1956-
units.registry[datetime.datetime] = converter(
1957-
interval_multiples=interval_multiples)
1923+
conv_st = 'auto'
1924+
int_mult = True
1925+
1926+
@classmethod
1927+
def set_converter(cls, s):
1928+
"""Called by validator for rcParams date.converter"""
1929+
cls.conv_st = s
1930+
cls.register_converters()
1931+
1932+
@classmethod
1933+
def set_int_mult(cls, b):
1934+
"""Called by validator for rcParams date.interval_multiples"""
1935+
cls.int_mult = b
1936+
cls.register_converters()
19581937

1959-
# _register_converters()
1938+
@classmethod
1939+
def register_converters(cls):
1940+
"""
1941+
Helper to register the date converters when rcParams `date.converter` and
1942+
`date.interval_multiples` are changed. Called by the helpers above.
1943+
"""
1944+
if cls.conv_st == 'concise':
1945+
converter = ConciseDateConverter
1946+
else:
1947+
converter = DateConverter
1948+
1949+
interval_multiples = cls.int_mult
1950+
print(interval_multiples)
1951+
units.registry[np.datetime64] = converter(
1952+
interval_multiples=interval_multiples)
1953+
units.registry[datetime.date] = converter(
1954+
interval_multiples=interval_multiples)
1955+
units.registry[datetime.datetime] = converter(
1956+
interval_multiples=interval_multiples)

lib/matplotlib/rcsetup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def _validate_date_converter(s):
178178
s = validate_string(s)
179179
try:
180180
import matplotlib.dates as mdates
181-
mdates._set_converter(s)
181+
mdates._rcParam_helper.set_converter(s)
182182
except Exception as e:
183183
pass
184184

@@ -189,7 +189,7 @@ def _validate_date_int_mult(s):
189189
s = validate_bool(s)
190190
try:
191191
import matplotlib.dates as mdates
192-
mdates._set_int_mult(s)
192+
mdates._rcParam_helper.set_int_mult(s)
193193
except Exception as e:
194194
pass
195195

lib/matplotlib/tests/test_dates.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,3 +947,43 @@ def test_change_epoch():
947947
np.testing.assert_allclose(
948948
mdates.date2num(np.datetime64('1970-01-01T12:00:00')),
949949
0.5)
950+
951+
952+
def test_change_converter():
953+
954+
plt.rcParams['date.converter'] = 'concise'
955+
dates = np.arange('2020-01-01', '2020-05-01', dtype='datetime64[D]')
956+
fig, ax = plt.subplots()
957+
958+
ax.plot(dates, np.arange(len(dates)))
959+
fig.canvas.draw()
960+
assert ax.get_xticklabels()[0].get_text() == 'Jan'
961+
assert ax.get_xticklabels()[1].get_text() == '15'
962+
963+
plt.rcParams['date.converter'] = 'auto'
964+
fig, ax = plt.subplots()
965+
966+
ax.plot(dates, np.arange(len(dates)))
967+
fig.canvas.draw()
968+
assert ax.get_xticklabels()[0].get_text() == 'Jan 01 2020'
969+
assert ax.get_xticklabels()[1].get_text() == 'Jan 15 2020'
970+
971+
972+
def test_change_interval_multiples():
973+
974+
plt.rcParams['date.interval_multiples'] = False
975+
dates = np.arange('2020-01-10', '2020-05-01', dtype='datetime64[D]')
976+
fig, ax = plt.subplots()
977+
978+
ax.plot(dates, np.arange(len(dates)))
979+
fig.canvas.draw()
980+
assert ax.get_xticklabels()[0].get_text() == 'Jan 10 2020'
981+
assert ax.get_xticklabels()[1].get_text() == 'Jan 24 2020'
982+
983+
plt.rcParams['date.interval_multiples'] = 'True'
984+
fig, ax = plt.subplots()
985+
986+
ax.plot(dates, np.arange(len(dates)))
987+
fig.canvas.draw()
988+
assert ax.get_xticklabels()[0].get_text() == 'Jan 15 2020'
989+
assert ax.get_xticklabels()[1].get_text() == 'Feb 01 2020'

0 commit comments

Comments
 (0)