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

Skip to content

Commit 78dda48

Browse files
committed
Apply un_convert to ginput(), get_xlim(), set_xlim()
1 parent 185e95b commit 78dda48

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
@@ -3041,7 +3041,16 @@ def get_xlim(self):
30413041
be greater than the `right` value.
30423042
30433043
"""
3044-
return tuple(self.viewLim.intervalx)
3044+
return self._convert_back_lim(self.viewLim.intervalx, self.xaxis)
3045+
3046+
def _convert_back_lim(self, lim, axis):
3047+
"""
3048+
Helper method to convert axis limits back to unitized data.
3049+
"""
3050+
if axis.converter is not None:
3051+
lim = [axis.converter.un_convert(l, axis.units, axis) for
3052+
l in lim]
3053+
return tuple(lim)
30453054

30463055
def _validate_converted_limits(self, limit, convert):
30473056
"""
@@ -3393,6 +3402,7 @@ def get_ylim(self):
33933402
will be greater than the `top` value.
33943403
33953404
"""
3405+
return self._convert_back_lim(self.viewLim.intervaly, self.yaxis)
33963406
return tuple(self.viewLim.intervaly)
33973407

33983408
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
@@ -180,12 +180,14 @@ def mouse_event_pop(self, event):
180180

181181
def add_click(self, event):
182182
"""Add the coordinates of an event to the list of clicks."""
183-
self.clicks.append((event.xdata, event.ydata))
183+
x = event.inaxes.xaxis.unconvert_units(event.xdata)
184+
y = event.inaxes.yaxis.unconvert_units(event.ydata)
185+
self.clicks.append((x, y))
184186
_log.info("input %i: %f, %f",
185-
len(self.clicks), event.xdata, event.ydata)
187+
len(self.clicks), x, y)
186188
# If desired, plot up click.
187189
if self.show_clicks:
188-
line = mlines.Line2D([event.xdata], [event.ydata],
190+
line = mlines.Line2D([x], [y],
189191
marker='+', color='r')
190192
event.inaxes.add_line(line)
191193
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)