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

Skip to content

Commit 353ceb4

Browse files
committed
FIX: empty data assymetry
1 parent a26f9a3 commit 353ceb4

File tree

5 files changed

+109
-25
lines changed

5 files changed

+109
-25
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2863,8 +2863,8 @@ def autoscale_view(self, tight=None, scalex=True, scaley=True):
28632863
if self.get_yscale() == 'log':
28642864
y_stickies = y_stickies[y_stickies > 0]
28652865

2866-
def handle_single_axis(scale, autoscaleon, shared_axes, interval,
2867-
minpos, axis, margin, stickies, set_bound):
2866+
def handle_single_axis(scale, autoscaleon, shared_axes, name,
2867+
axis, margin, stickies, set_bound):
28682868

28692869
if not (scale and autoscaleon):
28702870
return # nothing to do...
@@ -2876,12 +2876,16 @@ def handle_single_axis(scale, autoscaleon, shared_axes, interval,
28762876
x_values = []
28772877
minimum_minpos = np.inf
28782878
for ax in shared:
2879-
x_values.extend(getattr(ax.dataLim, interval))
2879+
x_values.extend(getattr(ax.dataLim, f"interval{name}"))
28802880
minimum_minpos = min(minimum_minpos,
2881-
getattr(ax.dataLim, minpos))
2881+
getattr(ax.dataLim, f"minpos{name}"))
28822882
x_values = np.extract(np.isfinite(x_values), x_values)
28832883
if x_values.size >= 1:
28842884
x0, x1 = (x_values.min(), x_values.max())
2885+
elif getattr(self._viewLim, f"mutated{name}")():
2886+
# No data, but explicit viewLims already set:
2887+
# in mutatedx or mutatedy.
2888+
return
28852889
else:
28862890
x0, x1 = (-np.inf, np.inf)
28872891
# If x0 and x1 are non finite, use the locator to figure out
@@ -2925,11 +2929,11 @@ def handle_single_axis(scale, autoscaleon, shared_axes, interval,
29252929
# End of definition of internal function 'handle_single_axis'.
29262930

29272931
handle_single_axis(
2928-
scalex, self._autoscaleXon, self._shared_x_axes, 'intervalx',
2929-
'minposx', self.xaxis, self._xmargin, x_stickies, self.set_xbound)
2932+
scalex, self._autoscaleXon, self._shared_x_axes, 'x',
2933+
self.xaxis, self._xmargin, x_stickies, self.set_xbound)
29302934
handle_single_axis(
2931-
scaley, self._autoscaleYon, self._shared_y_axes, 'intervaly',
2932-
'minposy', self.yaxis, self._ymargin, y_stickies, self.set_ybound)
2935+
scaley, self._autoscaleYon, self._shared_y_axes, 'y',
2936+
self.yaxis, self._ymargin, y_stickies, self.set_ybound)
29332937

29342938
def _get_axis_list(self):
29352939
return self.xaxis, self.yaxis

lib/matplotlib/axis.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,7 +1457,7 @@ def update_units(self, data):
14571457
if default is not None and self.units is None:
14581458
self.set_units(default)
14591459

1460-
if neednew:
1460+
elif neednew:
14611461
self._update_axisinfo()
14621462
self.stale = True
14631463
return True
@@ -2267,17 +2267,15 @@ def set_inverted(self, inverted):
22672267
def set_default_intervals(self):
22682268
# docstring inherited
22692269
xmin, xmax = 0., 1.
2270-
dataMutated = self.axes.dataLim.mutatedx()
2271-
viewMutated = self.axes.viewLim.mutatedx()
2272-
if not dataMutated or not viewMutated:
2270+
# only change view if dataLim has not changed and user has
2271+
# not changed the view:
2272+
if (not self.axes.dataLim.mutatedx() and
2273+
not self.axes.viewLim.mutatedx()):
22732274
if self.converter is not None:
22742275
info = self.converter.axisinfo(self.units, self)
22752276
if info.default_limits is not None:
22762277
xmin, xmax = self.convert_units(info.default_limits)
2277-
if not dataMutated:
2278-
self.axes.dataLim.intervalx = xmin, xmax
2279-
if not viewMutated:
2280-
self.axes.viewLim.intervalx = xmin, xmax
2278+
self.axes.viewLim.intervalx = xmin, xmax
22812279
self.stale = True
22822280

22832281
def get_tick_space(self):
@@ -2530,17 +2528,15 @@ def set_inverted(self, inverted):
25302528
def set_default_intervals(self):
25312529
# docstring inherited
25322530
ymin, ymax = 0., 1.
2533-
dataMutated = self.axes.dataLim.mutatedy()
2534-
viewMutated = self.axes.viewLim.mutatedy()
2535-
if not dataMutated or not viewMutated:
2531+
# only change view if dataLim has not changed and user has
2532+
# not changed the view:
2533+
if (not self.axes.dataLim.mutatedy() and
2534+
not self.axes.viewLim.mutatedy()):
25362535
if self.converter is not None:
25372536
info = self.converter.axisinfo(self.units, self)
25382537
if info.default_limits is not None:
25392538
ymin, ymax = self.convert_units(info.default_limits)
2540-
if not dataMutated:
2541-
self.axes.dataLim.intervaly = ymin, ymax
2542-
if not viewMutated:
2543-
self.axes.viewLim.intervaly = ymin, ymax
2539+
self.axes.viewLim.intervaly = ymin, ymax
25442540
self.stale = True
25452541

25462542
def get_tick_space(self):
Binary file not shown.

lib/matplotlib/tests/test_dates.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,55 @@ def test_date2num_NaT_scalar(units):
7070
assert np.isnan(tmpl)
7171

7272

73-
@image_comparison(['date_empty.png'])
7473
def test_date_empty():
7574
# make sure we do the right thing when told to plot dates even
7675
# if no date data has been presented, cf
7776
# http://sourceforge.net/tracker/?func=detail&aid=2850075&group_id=80706&atid=560720
7877
fig, ax = plt.subplots()
7978
ax.xaxis_date()
79+
fig.draw_no_output()
80+
np.testing.assert_allclose(ax.get_xlim(),
81+
[mdates.date2num(np.datetime64('2000-01-01')),
82+
mdates.date2num(np.datetime64('2010-01-01'))])
83+
84+
mdates._reset_epoch_test_example()
85+
mdates.set_epoch('0000-12-31')
86+
fig, ax = plt.subplots()
87+
ax.xaxis_date()
88+
fig.draw_no_output()
89+
np.testing.assert_allclose(ax.get_xlim(),
90+
[mdates.date2num(np.datetime64('2000-01-01')),
91+
mdates.date2num(np.datetime64('2010-01-01'))])
92+
mdates._reset_epoch_test_example()
93+
94+
95+
def test_date_not_empty():
96+
fig = plt.figure()
97+
ax = fig.add_subplot()
98+
99+
ax.plot([50, 70], [1, 2])
100+
ax.xaxis.axis_date()
101+
np.testing.assert_allclose(ax.get_xlim(), [50, 70])
102+
103+
104+
def test_axhline():
105+
# make sure that axhline doesn't set the xlimits...
106+
fig, ax = plt.subplots()
107+
ax.axhline(1.5)
108+
ax.plot([np.datetime64('2016-01-01'), np.datetime64('2016-01-02')], [1, 2])
109+
np.testing.assert_allclose(ax.get_xlim(),
110+
[mdates.date2num(np.datetime64('2016-01-01')),
111+
mdates.date2num(np.datetime64('2016-01-02'))])
112+
113+
mdates._reset_epoch_test_example()
114+
mdates.set_epoch('0000-12-31')
115+
fig, ax = plt.subplots()
116+
ax.axhline(1.5)
117+
ax.plot([np.datetime64('2016-01-01'), np.datetime64('2016-01-02')], [1, 2])
118+
np.testing.assert_allclose(ax.get_xlim(),
119+
[mdates.date2num(np.datetime64('2016-01-01')),
120+
mdates.date2num(np.datetime64('2016-01-02'))])
121+
mdates._reset_epoch_test_example()
80122

81123

82124
@image_comparison(['date_axhspan.png'])

lib/matplotlib/tests/test_units.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ def default_units(value, axis):
6767
return None
6868

6969
qc.convert = MagicMock(side_effect=convert)
70-
qc.axisinfo = MagicMock(side_effect=lambda u, a: munits.AxisInfo(label=u))
70+
qc.axisinfo = MagicMock(side_effect=lambda u, a:
71+
munits.AxisInfo(label=u, default_limits=(0, 100)))
7172
qc.default_units = MagicMock(side_effect=default_units)
7273
return qc
7374

@@ -219,3 +220,44 @@ def test_shared_axis_categorical():
219220
ax2.plot(d2.keys(), d2.values())
220221
ax1.xaxis.set_units(UnitData(["c", "d"]))
221222
assert "c" in ax2.xaxis.get_units()._mapping.keys()
223+
224+
225+
def test_empty_default_limits(quantity_converter):
226+
munits.registry[Quantity] = quantity_converter
227+
fig, ax1 = plt.subplots()
228+
ax1.xaxis.update_units(Quantity([10], "miles"))
229+
fig.draw_no_output()
230+
assert ax1.get_xlim() == (0, 100)
231+
ax1.yaxis.update_units(Quantity([10], "miles"))
232+
fig.draw_no_output()
233+
assert ax1.get_ylim() == (0, 100)
234+
235+
fig, ax = plt.subplots()
236+
ax.axhline(30)
237+
ax.plot(Quantity(np.arange(0, 3), "miles"),
238+
Quantity(np.arange(0, 6, 2), "feet"))
239+
fig.draw_no_output()
240+
assert ax.get_xlim() == (0, 2)
241+
assert ax.get_ylim() == (0, 30)
242+
243+
fig, ax = plt.subplots()
244+
ax.axvline(30)
245+
ax.plot(Quantity(np.arange(0, 3), "miles"),
246+
Quantity(np.arange(0, 6, 2), "feet"))
247+
fig.draw_no_output()
248+
assert ax.get_xlim() == (0, 30)
249+
assert ax.get_ylim() == (0, 4)
250+
251+
fig, ax = plt.subplots()
252+
ax.xaxis.update_units(Quantity([10], "miles"))
253+
ax.axhline(30)
254+
fig.draw_no_output()
255+
assert ax.get_xlim() == (0, 100)
256+
assert ax.get_ylim() == (28.5, 31.5)
257+
258+
fig, ax = plt.subplots()
259+
ax.yaxis.update_units(Quantity([10], "miles"))
260+
ax.axvline(30)
261+
fig.draw_no_output()
262+
assert ax.get_ylim() == (0, 100)
263+
assert ax.get_xlim() == (28.5, 31.5)

0 commit comments

Comments
 (0)