From 22e7a33158c64fae347a2f5918298c8fe3df0567 Mon Sep 17 00:00:00 2001 From: James Evans Date: Tue, 11 Aug 2015 08:35:21 -0700 Subject: [PATCH 1/2] JRE - Text would incorrectly strip away units when querying the position after it was set. This fixes Text such that it will return the original data when asked and will only strip away the units when it is needed. --- lib/matplotlib/text.py | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index 92f17a4c9f8f..d770d4243839 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -280,7 +280,7 @@ def contains(self, mouseevent): def _get_xy_display(self): 'get the (possibly unit converted) transformed x, y in display coords' - x, y = self.get_position() + x, y = self._get_unitless_position() return self.get_transform().transform_point((x, y)) def _get_multialignment(self): @@ -536,8 +536,8 @@ def update_bbox_position_size(self, renderer): trans = self.get_transform() - # don't use self.get_position here, which refers to text position - # in Text, and dash position in TextWithDash: + # don't use self._get_unitless_position here, which refers to text + # position in Text, and dash position in TextWithDash: posx = float(self.convert_xunits(self._x)) posy = float(self.convert_yunits(self._y)) @@ -877,12 +877,20 @@ def get_horizontalalignment(self): """ return self._horizontalalignment - def get_position(self): - "Return the position of the text as a tuple (*x*, *y*)" + def _get_unitless_position(self): + "Return the unitless position of the text as a tuple (*x*, *y*)" + # This will get the position with all unit information stripped away. + # This is here for convienience since it is done in several locations. x = float(self.convert_xunits(self._x)) y = float(self.convert_yunits(self._y)) return x, y + def get_position(self): + "Return the position of the text as a tuple (*x*, *y*)" + # This should return the same data (possible unitized) as was + # specified with 'set_x' and 'set_y'. + return self._x, self._y + def get_prop_tup(self): """ Return a hashable tuple of properties. @@ -891,7 +899,7 @@ def get_prop_tup(self): want to cache derived information about text (e.g., layouts) and need to know if the text has changed. """ - x, y = self.get_position() + x, y = self._get_unitless_position() return (x, y, self.get_text(), self._color, self._verticalalignment, self._horizontalalignment, hash(self._fontproperties), @@ -950,7 +958,7 @@ def get_window_extent(self, renderer=None, dpi=None): raise RuntimeError('Cannot get window extent w/o renderer') bbox, info, descent = self._get_layout(self._renderer) - x, y = self.get_position() + x, y = self._get_unitless_position() x, y = self.get_transform().transform_point((x, y)) bbox = bbox.translated(x, y) if dpi is not None: @@ -1365,12 +1373,20 @@ def __init__(self, #self.set_bbox(dict(pad=0)) - def get_position(self): - "Return the position of the text as a tuple (*x*, *y*)" + def _get_unitless_position(self): + "Return the unitless position of the text as a tuple (*x*, *y*)" + # This will get the position with all unit information stripped away. + # This is here for convienience since it is done in several locations. x = float(self.convert_xunits(self._dashx)) y = float(self.convert_yunits(self._dashy)) return x, y + def get_position(self): + "Return the position of the text as a tuple (*x*, *y*)" + # This should return the same data (possibly unitized) as was + # specified with set_x and set_y + return self._dashx, self._dashy + def get_prop_tup(self): """ Return a hashable tuple of properties. @@ -1402,7 +1418,7 @@ def update_coords(self, renderer): with respect to the actual canvas's coordinates we need to map back and forth. """ - dashx, dashy = self.get_position() + dashx, dashy = self._get_unitless_position() dashlength = self.get_dashlength() # Shortcircuit this process if we don't have a dash if dashlength == 0.0: From 9cb294602ec348bbb229ac5d931a50dc57e79536 Mon Sep 17 00:00:00 2001 From: James Evans Date: Mon, 17 Aug 2015 13:24:58 -0700 Subject: [PATCH 2/2] Added an 'api_changes' entry Renamed '_get_unitless_position' to 'get_unitless_position' --- doc/api/api_changes/2015-08-17-JRE.rst | 11 +++++++++++ lib/matplotlib/text.py | 14 +++++++------- 2 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 doc/api/api_changes/2015-08-17-JRE.rst diff --git a/doc/api/api_changes/2015-08-17-JRE.rst b/doc/api/api_changes/2015-08-17-JRE.rst new file mode 100644 index 000000000000..0f15fbac4852 --- /dev/null +++ b/doc/api/api_changes/2015-08-17-JRE.rst @@ -0,0 +1,11 @@ + Preserve units with Text position + ````````````````````````````````` + +Previously the 'get_position' method on Text would strip away unit information +even though the units were still present. There was no inherent need to do +this, so it has been changed so that unit data (if present) will be preserved. +Essentially a call to 'get_position' will return the exact value from a call to +'set_position'. + +If you wish to get the old behaviour, then you can use the new method called +'get_unitless_position'. diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index d770d4243839..cd0ddb570c18 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -280,7 +280,7 @@ def contains(self, mouseevent): def _get_xy_display(self): 'get the (possibly unit converted) transformed x, y in display coords' - x, y = self._get_unitless_position() + x, y = self.get_unitless_position() return self.get_transform().transform_point((x, y)) def _get_multialignment(self): @@ -536,7 +536,7 @@ def update_bbox_position_size(self, renderer): trans = self.get_transform() - # don't use self._get_unitless_position here, which refers to text + # don't use self.get_unitless_position here, which refers to text # position in Text, and dash position in TextWithDash: posx = float(self.convert_xunits(self._x)) posy = float(self.convert_yunits(self._y)) @@ -877,7 +877,7 @@ def get_horizontalalignment(self): """ return self._horizontalalignment - def _get_unitless_position(self): + def get_unitless_position(self): "Return the unitless position of the text as a tuple (*x*, *y*)" # This will get the position with all unit information stripped away. # This is here for convienience since it is done in several locations. @@ -899,7 +899,7 @@ def get_prop_tup(self): want to cache derived information about text (e.g., layouts) and need to know if the text has changed. """ - x, y = self._get_unitless_position() + x, y = self.get_unitless_position() return (x, y, self.get_text(), self._color, self._verticalalignment, self._horizontalalignment, hash(self._fontproperties), @@ -958,7 +958,7 @@ def get_window_extent(self, renderer=None, dpi=None): raise RuntimeError('Cannot get window extent w/o renderer') bbox, info, descent = self._get_layout(self._renderer) - x, y = self._get_unitless_position() + x, y = self.get_unitless_position() x, y = self.get_transform().transform_point((x, y)) bbox = bbox.translated(x, y) if dpi is not None: @@ -1373,7 +1373,7 @@ def __init__(self, #self.set_bbox(dict(pad=0)) - def _get_unitless_position(self): + def get_unitless_position(self): "Return the unitless position of the text as a tuple (*x*, *y*)" # This will get the position with all unit information stripped away. # This is here for convienience since it is done in several locations. @@ -1418,7 +1418,7 @@ def update_coords(self, renderer): with respect to the actual canvas's coordinates we need to map back and forth. """ - dashx, dashy = self._get_unitless_position() + dashx, dashy = self.get_unitless_position() dashlength = self.get_dashlength() # Shortcircuit this process if we don't have a dash if dashlength == 0.0: