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

Skip to content

Commit f5f7877

Browse files
committed
Apply un_convert to ginput(), get_xlim(), set_xlim()
1 parent 04a036f commit f5f7877

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3045,7 +3045,16 @@ def get_xlim(self):
30453045
be greater than the `right` value.
30463046
30473047
"""
3048-
return tuple(self.viewLim.intervalx)
3048+
return self._convert_back_lim(self.viewLim.intervalx, self.xaxis)
3049+
3050+
def _convert_back_lim(self, lim, axis):
3051+
"""
3052+
Helper method to convert axis limits back to unitized data.
3053+
"""
3054+
if axis.converter is not None:
3055+
lim = [axis.converter.un_convert(l, axis.units, axis) for
3056+
l in lim]
3057+
return tuple(lim)
30493058

30503059
def _validate_converted_limits(self, limit, convert):
30513060
"""
@@ -3397,6 +3406,7 @@ def get_ylim(self):
33973406
will be greater than the `top` value.
33983407
33993408
"""
3409+
return self._convert_back_lim(self.viewLim.intervaly, self.yaxis)
34003410
return tuple(self.viewLim.intervaly)
34013411

34023412
def set_ylim(self, bottom=None, top=None, emit=True, auto=False,

lib/matplotlib/axis.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,6 +1537,11 @@ def convert_units(self, x):
15371537
ret = self.converter.convert(x, self.units, self)
15381538
return ret
15391539

1540+
def unconvert_units(self, x):
1541+
if self.converter is None:
1542+
return x
1543+
return self.converter.un_convert(x, self.units, self)
1544+
15401545
def set_units(self, u):
15411546
"""
15421547
Set the units for axis.

lib/matplotlib/blocking_input.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,14 @@ def add_click(self, event):
204204
----------
205205
event : `~.backend_bases.MouseEvent`
206206
"""
207-
self.clicks.append((event.xdata, event.ydata))
207+
x = event.inaxes.xaxis.unconvert_units(event.xdata)
208+
y = event.inaxes.yaxis.unconvert_units(event.ydata)
209+
self.clicks.append((x, y))
208210
_log.info("input %i: %f, %f",
209-
len(self.clicks), event.xdata, event.ydata)
211+
len(self.clicks), x, y)
210212
# If desired, plot up click.
211213
if self.show_clicks:
212-
line = mlines.Line2D([event.xdata], [event.ydata],
214+
line = mlines.Line2D([x], [y],
213215
marker='+', color='r')
214216
event.inaxes.add_line(line)
215217
self.marks.append(line)

lib/matplotlib/tests/test_axes.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5815,3 +5815,14 @@ class DummySubplot(matplotlib.axes.SubplotBase, Dummy):
58155815
FactoryDummySubplot = matplotlib.axes.subplot_class_factory(Dummy)
58165816

58175817
assert DummySubplot is FactoryDummySubplot
5818+
5819+
5820+
def test_lim_units():
5821+
# Check that get_(x,y)lim() returns unitized data
5822+
fig, ax = plt.subplots()
5823+
ax.scatter([datetime.datetime(2000, 1, 1)],
5824+
[datetime.datetime(2000, 1, 1)])
5825+
xlim = ax.get_xlim()
5826+
ylim = ax.get_ylim()
5827+
for l in xlim + ylim:
5828+
assert type(l) is datetime.datetime

0 commit comments

Comments
 (0)