Skip to content

Use data to set default limits when appropriate #18803

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1477,11 +1477,21 @@ def update_units(self, data):
self.set_units(default)

if neednew:
self._update_axisinfo()
if np.iterable(data):
try:
data_interval = (np.min(data), np.max(data))
except (TypeError, ValueError):
data_interval = None
else:
if data_interval[0] == data_interval[1]:
data_interval = None
else:
data_interval = None
self._update_axisinfo(data_interval)
self.stale = True
return True

def _update_axisinfo(self):
def _update_axisinfo(self, data_interval=None):
"""
Check the axis converter for the stored units to see if the
axis info needs to be updated.
Expand Down Expand Up @@ -1513,7 +1523,7 @@ def _update_axisinfo(self):
self.set_label_text(info.label)
self.isDefault_label = True

self.set_default_intervals()
self.set_default_intervals(data_interval)

def have_units(self):
return self.converter is not None or self.units is not None
Expand Down Expand Up @@ -2277,15 +2287,18 @@ def set_inverted(self, inverted):
# cast to bool to avoid bad interaction between python 3.8 and np.bool_
self.axes.set_xlim(sorted((a, b), reverse=bool(inverted)), auto=None)

def set_default_intervals(self):
def set_default_intervals(self, data_interval=None):
# docstring inherited
xmin, xmax = 0., 1.
xmin, xmax = (0., 1.) if data_interval is None else data_interval
dataMutated = self.axes.dataLim.mutatedx()
viewMutated = self.axes.viewLim.mutatedx()
if not dataMutated or not viewMutated:
if self.converter is not None:
info = self.converter.axisinfo(self.units, self)
if info.default_limits is not None:
if data_interval is not None:
xmin = self.converter.convert(xmin, self.units, self)
xmax = self.converter.convert(xmax, self.units, self)
elif info.default_limits is not None:
valmin, valmax = info.default_limits
xmin = self.converter.convert(valmin, self.units, self)
xmax = self.converter.convert(valmax, self.units, self)
Expand Down Expand Up @@ -2561,15 +2574,18 @@ def set_inverted(self, inverted):
# cast to bool to avoid bad interaction between python 3.8 and np.bool_
self.axes.set_ylim(sorted((a, b), reverse=bool(inverted)), auto=None)

def set_default_intervals(self):
def set_default_intervals(self, data_interval=None):
# docstring inherited
ymin, ymax = 0., 1.
ymin, ymax = (0., 1.) if data_interval is None else data_interval
dataMutated = self.axes.dataLim.mutatedy()
viewMutated = self.axes.viewLim.mutatedy()
if not dataMutated or not viewMutated:
if self.converter is not None:
info = self.converter.axisinfo(self.units, self)
if info.default_limits is not None:
if data_interval is not None:
ymin = self.converter.convert(ymin, self.units, self)
ymax = self.converter.convert(ymax, self.units, self)
elif info.default_limits is not None:
valmin, valmax = info.default_limits
ymin = self.converter.convert(valmin, self.units, self)
ymax = self.converter.convert(valmax, self.units, self)
Expand Down
43 changes: 43 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6824,3 +6824,46 @@ def test_ylabel_ha_with_position(ha):
ax.set_ylabel("test", y=1, ha=ha)
ax.yaxis.set_label_position("right")
assert ax.yaxis.get_label().get_ha() == ha


def test_axhline_with_date_xaxis():
fig, ax = plt.subplots()
ax.axhline(1.5)
x = [datetime.datetime(2020, 1, i) for i in range(1, 3)]
y = [1, 2]
ax.plot(x, y)
assert ax.get_xlim() == (18262.0, 18263.0)


def test_axvline_with_date_yaxis():
fig, ax = plt.subplots()
ax.axvline(1.5)
x = [1, 2]
y = [datetime.datetime(2020, 1, i) for i in range(1, 3)]
ax.plot(x, y)
assert ax.get_ylim() == (18262.0, 18263.0)


def test_two_spans():
fig, ax = plt.subplots()
ax.axhline(1)
ax.axhline(2)
assert ax.get_ylim() == (1.0, 2.0)


def test_axhspan_with_date_xaxis():
fig, ax = plt.subplots()
ax.axhspan(1, 2)
x = [datetime.datetime(2020, 1, i) for i in range(1, 3)]
y = [1, 2]
ax.plot(x, y)
assert ax.get_xlim() == (18262.0, 18263.0)


def test_axvspan_with_date_yaxis():
fig, ax = plt.subplots()
ax.axvspan(1, 2)
x = [1, 2]
y = [datetime.datetime(2020, 1, i) for i in range(1, 3)]
ax.plot(x, y)
assert ax.get_ylim() == (18262.0, 18263.0)