From 6f5b690190e0b676cc68fb789106ea9f37464bf8 Mon Sep 17 00:00:00 2001 From: ryan Date: Fri, 18 Mar 2016 14:14:06 -0400 Subject: [PATCH 1/2] displace feature added --- lib/matplotlib/legend.py | 82 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index 3af482cf6033..2f92804b1a96 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -185,6 +185,7 @@ def __init__(self, parent, handles, labels, bbox_transform=None, # transform for the bbox frameon=None, # draw frame handler_map=None, + displace=None, ): """ - *parent*: the artist that contains the legend @@ -223,6 +224,7 @@ def __init__(self, parent, handles, labels, title the legend title bbox_to_anchor the bbox that the legend will be anchored. bbox_transform the transform for the bbox. transAxes if None. + displace the legend posistion will be anchored ================ ==================================================== @@ -394,6 +396,9 @@ def __init__(self, parent, handles, labels, self._last_fontsize_points = self._fontsize self._draggable = None + if displace != None: + self.set_displace(displace) + def _set_artist_props(self, a): """ set the boilerplate props for artists added to axes @@ -851,6 +856,83 @@ def get_bbox_to_anchor(self): else: return self._bbox_to_anchor + def set_displace(self, displace): + """ + Place a legend on the plot by given displace codes. + on the axes at location loc. Labels are a + sequence of strings and loc can be a string or an integer + specifying the legend location + + The displace code are:: + + 'Outside' : 'in', + 'Inner' : 'out', + 'Right Center' : 'RC', + 'Upper Center' : 'UC', + 'Left Center' : 'LC', + 'Bottom Center' : 'BC', + 'Upper Right' : 'UR', + 'Upper Left' : 'UL', + 'Bottom Right' : 'BR', + 'Bottom Left' : 'BL', + + by given inner and outside code before given position code. + The default setting with displace will be Inner Upper Right + + >>>plt.legend(displace['out','UL']) + it is an example for setting legend out side to the plot at + Upper Left conner + """ + + #displace_code return (bbox[0], bbox[1], loc) + out_displace_code = {"RC": (1.1, 0.5, 6), + "UC": (0.5, 1.1, 8), + "LC": (-0.1, 0.5, 5), + "BC": (0.5, -0.1, 9), + "UR": (1.1, 1, 2), + "UL": (-0.1, 1, 1), + "BR": (1.1, 0, 3), + "BL": (-0.1, 0, 4)} + + in_displace_code={"RC": (1, 0.5, 5), + "UC": (0.5, 1, 9), + "LC": (0, 0.5, 6), + "BC": (0.5, 0, 8), + "UR": (1, 1, 1), + "UL": (0, 1, 2), + "BR": (1, 0, 4), + "BL": (0, 0, 3)} + + #default displacement inner UpperRight + anchor = "in" + position = in_displace_code["UR"] + + try: + l = len(displace) + except TypeError: + raise ValueError("Invalid argument for displace : %s" % + str(displace)) + else: + for displacement in displace: + if displacement.lower() == "in": + anchor = "in" + elif displacement.lower() == "out": + anchor = "out" + else: + try: + if anchor == "in": + position = in_displace_code[displacement] + else: + position = out_displace_code[displacement] + except KeyError: + raise ValueError("Invalid argument for displace : %s" % + str(displacement)) + + self.set_bbox_to_anchor((position[0], position[1])) + self._set_loc(position[2]) + + self.stale = True + def set_bbox_to_anchor(self, bbox, transform=None): """ set the bbox that the legend will be anchored. From b3851441e9388a59080321c0163e2e1de68b3efd Mon Sep 17 00:00:00 2001 From: ryan Date: Fri, 18 Mar 2016 14:28:18 -0400 Subject: [PATCH 2/2] pep8 fix --- lib/matplotlib/legend.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index 2f92804b1a96..f707c7b37a74 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -224,7 +224,7 @@ def __init__(self, parent, handles, labels, title the legend title bbox_to_anchor the bbox that the legend will be anchored. bbox_transform the transform for the bbox. transAxes if None. - displace the legend posistion will be anchored + displace the legend posistion will be anchored ================ ==================================================== @@ -396,7 +396,7 @@ def __init__(self, parent, handles, labels, self._last_fontsize_points = self._fontsize self._draggable = None - if displace != None: + if displace is not None: self.set_displace(displace) def _set_artist_props(self, a): @@ -894,14 +894,14 @@ def set_displace(self, displace): "BR": (1.1, 0, 3), "BL": (-0.1, 0, 4)} - in_displace_code={"RC": (1, 0.5, 5), - "UC": (0.5, 1, 9), - "LC": (0, 0.5, 6), - "BC": (0.5, 0, 8), - "UR": (1, 1, 1), - "UL": (0, 1, 2), - "BR": (1, 0, 4), - "BL": (0, 0, 3)} + in_displace_code = {"RC": (1, 0.5, 5), + "UC": (0.5, 1, 9), + "LC": (0, 0.5, 6), + "BC": (0.5, 0, 8), + "UR": (1, 1, 1), + "UL": (0, 1, 2), + "BR": (1, 0, 4), + "BL": (0, 0, 3)} #default displacement inner UpperRight anchor = "in" @@ -931,7 +931,7 @@ def set_displace(self, displace): self.set_bbox_to_anchor((position[0], position[1])) self._set_loc(position[2]) - self.stale = True + self.stale = True def set_bbox_to_anchor(self, bbox, transform=None): """