8000 Mnt new default dates by jklymak · Pull Request #23188 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Mnt new default dates #23188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

8000
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions doc/api/next_api_changes/behavior/23188-JMK.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Default date limits changed to 1970-01-01 to 1970-01-02
-------------------------------------------------------

Previously the default limits for an empty axis set up for dates
(`.Axis.axis_date`) was 2000-01-01 to 2010-01-01. This has been
changed to 1970-01-01 to 1970-01-02. With the default epoch, this
makes the numeric limit for date axes the same as for other axes
(0.0-1.0), and users are less likely to set a locator with far too
many ticks.
20 changes: 10 additions & 10 deletions lib/matplotlib/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -1157,9 +1157,9 @@ def nonsingular(self, vmin, vmax):
if it is too close to being singular (i.e. a range of ~0).
"""
if not np.isfinite(vmin) or not np.isfinite(vmax):
# Except if there is no data, then use 2000-2010 as default.
return (date2num(datetime.date(2000, 1, 1)),
date2num(datetime.date(2010, 1, 1)))
# Except if there is no data, then use 1970 as default.
return (date2num(datetime.date(1970, 1, 1)),
date2num(datetime.date(1970, 1, 2)))
if vmax < vmin:
vmin, vmax = vmax, vmin
unit = self._get_unit()
Expand Down Expand Up @@ -1362,9 +1362,9 @@ def nonsingular(self, vmin, vmax):
# whatever is thrown at us, we can scale the unit.
# But default nonsingular date plots at an ~4 year period.
if not np.isfinite(vmin) or not np.isfinite(vmax):
# Except if there is no data, then use 2000-2010 as default.
return (date2num(datetime.date(2000, 1, 1)),
date2num(datetime.date(2010, 1, 1)))
# Except if there is no data, then use 1970 as default.
return (date2num(datetime.date(1970, 1, 1)),
date2num(datetime.date(1970, 1, 2)))
if vmax < vmin:
vmin, vmax = vmax, vmin
if vmin == vmax:
Expand Down Expand Up @@ -1850,8 +1850,8 @@ def axisinfo(self, unit, axis):
majloc = AutoDateLocator(tz=tz,
interval_multiples=self._interval_multiples)
majfmt = AutoDateFormatter(majloc, tz=tz)
datemin = datetime.date(2000, 1, 1)
datemax = datetime.date(2010, 1, 1)
datemin = datetime.date(1970, 1, 1)
datemax = datetime.date(1970, 1, 2)

return units.AxisInfo(majloc=majloc, majfmt=majfmt, label='',
default_limits=(datemin, datemax))
Expand Down Expand Up @@ -1907,8 +1907,8 @@ def axisinfo(self, unit, axis):
zero_formats=self._zero_formats,
offset_formats=self._offset_formats,
show_offset=self._show_offset)
datemin = datetime.date(2000, 1, 1)
datemax = datetime.date(2010, 1, 1)
datemin = datetime.date(1970, 1, 1)
datemax = datetime.date(1970, 1, 2)
return units.AxisInfo(majloc=majloc, majfmt=majfmt, label='',
default_limits=(datemin, datemax))

Expand Down
13 changes: 0 additions & 13 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7087,19 +7087,6 @@ def test_axis_extent_arg2():
assert (ymin, ymax) == ax.get_ylim()


def test_datetime_masked():
# make sure that all-masked data falls back to the viewlim
# set in convert.axisinfo....
x = np.array([datetime.datetime(2017, 1, n) for n in range(1, 6)])
y = np.array([1, 2, 3, 4, 5])
m = np.ma.masked_greater(y, 0)

fig, ax = plt.subplots()
ax.plot(x, m)
dt = mdates.date2num(np.datetime64('0000-12-31'))
assert ax.get_xlim() == (730120.0 + dt, 733773.0 + dt)


def test_hist_auto_bins():
_, bins, _ = plt.hist([[1, 2, 3], [3, 4, 5, 6]], bins='auto')
assert bins[0] <= 1
Expand Down
22 changes: 17 additions & 5 deletions lib/matplotlib/tests/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,17 @@ def test_date_empty():
ax.xaxis_date()
fig.draw_without_rendering()
np.testing.assert_allclose(ax.get_xlim(),
[mdates.date2num(np.datetime64('2000-01-01')),
mdates.date2num(np.datetime64('2010-01-01'))])
[mdates.date2num(np.datetime64('1970-01-01')),
mdates.date2num(np.datetime64('1970-01-02'))])

mdates._reset_epoch_test_example()
mdates.set_epoch('0000-12-31')
fig, ax = plt.subplots()
ax.xaxis_date()
fig.draw_without_rendering()
np.testing.assert_allclose(ax.get_xlim(),
[mdates.date2num(np.datetime64('2000-01-01')),
mdates.date2num(np.datetime64('2010-01-01'))])
[mdates.date2num(np.datetime64('1970-01-01')),
mdates.date2num(np.datetime64('1970-01-02'))])
mdates._reset_epoch_test_example()


Expand Down Expand Up @@ -1235,7 +1235,7 @@ def test_julian2num():
def test_DateLocator():
locator = mdates.DateLocator()
# Test nonsingular
assert locator.nonsingular(0, np.inf) == (10957.0, 14610.0)
assert locator.nonsingular(0, np.inf) == (0, 1)
assert locator.nonsingular(0, 1) == (0, 1)
assert locator.nonsingular(1, 0) == (0, 1)
assert locator.nonsingular(0, 0) == (-2, 2)
Expand Down Expand Up @@ -1328,3 +1328,15 @@ def test_usetex_newline():
fig, ax = plt.subplots()
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m\n%Y'))
fig.canvas.draw()


def test_datetime_masked():
# make sure that all-masked data falls back to the viewlim
# set in convert.axisinfo....
x = np.array([datetime.datetime(2017, 1, n) for n in range(1, 6)])
y = np.array([1, 2, 3, 4, 5])
m = np.ma.masked_greater(y, 0)

fig, ax = plt.subplots()
ax.plot(x, m)
assert ax.get_xlim() == (0, 1)
0