From cf3d0f20ae2fa204cf64a999630e15e881f97573 Mon Sep 17 00:00:00 2001 From: Wojciech Lange Date: Fri, 21 Dec 2018 10:02:21 +0100 Subject: [PATCH 01/26] #8236 --- lib/matplotlib/legend.py | 12 +++-- lib/matplotlib/legend_handler.py | 79 +++++++++++++++++++++++++++++++- 2 files changed, 86 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index fe6d70c8c529..1896d2962047 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -32,7 +32,7 @@ from matplotlib.cbook import silent_list, is_hashable, warn_deprecated from matplotlib.font_manager import FontProperties from matplotlib.lines import Line2D -from matplotlib.patches import Patch, Rectangle, Shadow, FancyBboxPatch +from matplotlib.patches import Patch, Rectangle, Shadow, FancyBboxPatch, FancyArrowPatch from matplotlib.collections import (LineCollection, RegularPolyCollection, CircleCollection, PathCollection, PolyCollection) @@ -43,6 +43,7 @@ from matplotlib.offsetbox import DraggableOffsetBox from matplotlib.container import ErrorbarContainer, BarContainer, StemContainer +from matplotlib.text import Annotation, Text from . import legend_handler @@ -678,7 +679,9 @@ def _approx_text_height(self, renderer=None): update_func=legend_handler.update_from_first_child), tuple: legend_handler.HandlerTuple(), PathCollection: legend_handler.HandlerPathCollection(), - PolyCollection: legend_handler.HandlerPolyCollection() + PolyCollection: legend_handler.HandlerPolyCollection(), + FancyArrowPatch: legend_handler.HandlerFancyArrowPatch(), + Annotation: legend_handler.HandlerAnnotation() } # (get|set|update)_default_handler_maps are public interfaces to @@ -865,6 +868,7 @@ def _init_legend_box(self, handles, labels, markerfirst=True): children=[self._legend_title_box, self._legend_handle_box]) self._legend_box.set_figure(self.figure) + self._legend_box.set_offset(self._findoffset) self.texts = text_list self.legendHandles = handle_list @@ -1219,12 +1223,12 @@ def _get_legend_handles(axs, legend_handler_map=None): handles_original = [] for ax in axs: handles_original += (ax.lines + ax.patches + - ax.collections + ax.containers) + ax.collections + ax.containers+ax.texts) # support parasite axes: if hasattr(ax, 'parasites'): for axx in ax.parasites: handles_original += (axx.lines + axx.patches + - axx.collections + axx.containers) + axx.collections + axx.containers+ax.texts) handler_map = Legend.get_default_handler_map() diff --git a/lib/matplotlib/legend_handler.py b/lib/matplotlib/legend_handler.py index 5d4e55a351da..561bf9a23bbf 100644 --- a/lib/matplotlib/legend_handler.py +++ b/lib/matplotlib/legend_handler.py @@ -29,8 +29,9 @@ def legend_artist(self, legend, orig_handle, fontsize, handlebox) import numpy as np from matplotlib.lines import Line2D -from matplotlib.patches import Rectangle +from matplotlib.patches import Rectangle, FancyArrowPatch import matplotlib.collections as mcoll +from matplotlib.text import Text, Annotation import matplotlib.colors as mcolors @@ -723,3 +724,79 @@ def create_artists(self, legend, orig_handle, self.update_prop(p, orig_handle, legend) p.set_transform(trans) return [p] + + + +class HandlerFancyArrowPatch(HandlerPatch): + """ + Handler for FancyArrowPatch instances. + """ + def _create_patch(self, legend, orig_handle, xdescent, ydescent, width, height, fontsize): + arrow = FancyArrowPatch([-xdescent, + -ydescent + height / 2], + [-xdescent + width, + -ydescent + height / 2], + mutation_scale=width / 3) + arrow.set_arrowstyle(orig_handle.get_arrowstyle()) + return arrow + + + + +class HandlerAnnotation(HandlerBase): + """ + Handler for Annotation instances. + Defers to HandlerFancyArrowPatch to draw the annotation arrow (if any). + Parameters + ---------- + pad : float, optional + If None, fall back to `legend.borderpad` asstr the default. + In units of fraction of font size. + Default is None. + width_ratios : tuple, optional + The relative width of the respective text/arrow legend annotation pair. + Must be of length 2. + Default is [1,4]. + """ + + def __init__( + self, + pad=None, + width_ratios=[1, 4], + **kwargs + ): + + self._pad = pad + self._width_ratios = width_ratios + + HandlerBase.__init__(self, **kwargs) + + def create_artists( + self, + legend, + orig_handle, + xdescent, + ydescent, + width, + height, + fontsize, + trans, + ): + + if orig_handle.arrow_patch is not None: + + # Arrow without text + + handler = HandlerFancyArrowPatch() + handle = orig_handle.arrow_patch + + return handler.create_artists( + legend, + handle, + xdescent, + ydescent, + width, + height, + fontsize, + trans, + ) \ No newline at end of file From a0b24218d82b692f1781704943b39940374081bc Mon Sep 17 00:00:00 2001 From: Wojciech Lange Date: Fri, 21 Dec 2018 13:06:39 +0100 Subject: [PATCH 02/26] doc --- examples/text_labels_and_annotations/legend.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/examples/text_labels_and_annotations/legend.py b/examples/text_labels_and_annotations/legend.py index 7e6162a51c25..71a04e477b01 100644 --- a/examples/text_labels_and_annotations/legend.py +++ b/examples/text_labels_and_annotations/legend.py @@ -21,6 +21,14 @@ ax.plot(a, d, 'k:', label='Data length') ax.plot(a, c + d, 'k', label='Total message length') + +#Creates an arrow with pre-defined label. +ax.annotate("", + xy=(1.5,4.5), + xytext=(1.5,9.0), + arrowprops={'arrowstyle':'<->', 'color':'C7' }, + label='distance') + legend = ax.legend(loc='upper center', shadow=True, fontsize='x-large') # Put a nicer background color on the legend. From 47de63bda1bcaa3e847b8aa965b458ed2900ea18 Mon Sep 17 00:00:00 2001 From: Wojciech Lange Date: Fri, 21 Dec 2018 18:22:44 +0100 Subject: [PATCH 03/26] Tweaks due to flake8 --- examples/text_labels_and_annotations/legend.py | 6 +++--- lib/matplotlib/legend.py | 3 ++- lib/matplotlib/legend_handler.py | 10 ++++------ 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/examples/text_labels_and_annotations/legend.py b/examples/text_labels_and_annotations/legend.py index 71a04e477b01..c80ffdb5c714 100644 --- a/examples/text_labels_and_annotations/legend.py +++ b/examples/text_labels_and_annotations/legend.py @@ -24,9 +24,9 @@ #Creates an arrow with pre-defined label. ax.annotate("", - xy=(1.5,4.5), - xytext=(1.5,9.0), - arrowprops={'arrowstyle':'<->', 'color':'C7' }, + xy=(1.5,4.5), + xytext=(1.5,9.0), + arrowprops={'arrowstyle':'<->', 'color':'C7'}, label='distance') legend = ax.legend(loc='upper center', shadow=True, fontsize='x-large') diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index 1896d2962047..58881d72820c 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -32,7 +32,8 @@ from matplotlib.cbook import silent_list, is_hashable, warn_deprecated from matplotlib.font_manager import FontProperties from matplotlib.lines import Line2D -from matplotlib.patches import Patch, Rectangle, Shadow, FancyBboxPatch, FancyArrowPatch +from matplotlib.patches import (Patch, Rectangle, Shadow, FancyBboxPatch, + FancyArrowPatch) from matplotlib.collections import (LineCollection, RegularPolyCollection, CircleCollection, PathCollection, PolyCollection) diff --git a/lib/matplotlib/legend_handler.py b/lib/matplotlib/legend_handler.py index 561bf9a23bbf..01066df3a550 100644 --- a/lib/matplotlib/legend_handler.py +++ b/lib/matplotlib/legend_handler.py @@ -726,12 +726,12 @@ def create_artists(self, legend, orig_handle, return [p] - class HandlerFancyArrowPatch(HandlerPatch): """ Handler for FancyArrowPatch instances. """ - def _create_patch(self, legend, orig_handle, xdescent, ydescent, width, height, fontsize): + def _create_patch(self, legend, orig_handle, xdescent, ydescent, width, + height, fontsize): arrow = FancyArrowPatch([-xdescent, -ydescent + height / 2], [-xdescent + width, @@ -741,8 +741,6 @@ def _create_patch(self, legend, orig_handle, xdescent, ydescent, width, height, return arrow - - class HandlerAnnotation(HandlerBase): """ Handler for Annotation instances. @@ -782,14 +780,14 @@ def create_artists( fontsize, trans, ): - + if orig_handle.arrow_patch is not None: # Arrow without text handler = HandlerFancyArrowPatch() handle = orig_handle.arrow_patch - + return handler.create_artists( legend, handle, From 07387d27fcdf80822727c7cb0245b29e06ff86ed Mon Sep 17 00:00:00 2001 From: Wojciech Lange Date: Thu, 14 Feb 2019 11:25:24 +0100 Subject: [PATCH 04/26] flake8 --- examples/text_labels_and_annotations/legend.py | 6 ++---- lib/matplotlib/legend_handler.py | 12 ++++++------ 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/examples/text_labels_and_annotations/legend.py b/examples/text_labels_and_annotations/legend.py index c80ffdb5c714..eb1bf0c23ef1 100644 --- a/examples/text_labels_and_annotations/legend.py +++ b/examples/text_labels_and_annotations/legend.py @@ -24,10 +24,8 @@ #Creates an arrow with pre-defined label. ax.annotate("", - xy=(1.5,4.5), - xytext=(1.5,9.0), - arrowprops={'arrowstyle':'<->', 'color':'C7'}, - label='distance') + xy=(1.5,4.5), xytext=(1.5,9.0), + arrowprops={'arrowstyle':'<->', 'color':'C7'}, label='distance') legend = ax.legend(loc='upper center', shadow=True, fontsize='x-large') diff --git a/lib/matplotlib/legend_handler.py b/lib/matplotlib/legend_handler.py index 01066df3a550..0c6611c60f37 100644 --- a/lib/matplotlib/legend_handler.py +++ b/lib/matplotlib/legend_handler.py @@ -748,13 +748,13 @@ class HandlerAnnotation(HandlerBase): Parameters ---------- pad : float, optional - If None, fall back to `legend.borderpad` asstr the default. - In units of fraction of font size. - Default is None. + If None, fall back to `legend.borderpad` asstr the default. + In units of fraction of font size. + Default is None. width_ratios : tuple, optional - The relative width of the respective text/arrow legend annotation pair. - Must be of length 2. - Default is [1,4]. + The relative width of the respective text/arrow legend annotation pair. + Must be of length 2. + Default is [1,4]. """ def __init__( From 15e780b4b0937a26ad679b90acf3fb8192a7bb22 Mon Sep 17 00:00:00 2001 From: Wojciech Lange Date: Thu, 14 Feb 2019 13:20:48 +0100 Subject: [PATCH 05/26] flake8 --- examples/text_labels_and_annotations/legend.py | 4 ++-- lib/matplotlib/legend_handler.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/text_labels_and_annotations/legend.py b/examples/text_labels_and_annotations/legend.py index eb1bf0c23ef1..b9543f2c1eab 100644 --- a/examples/text_labels_and_annotations/legend.py +++ b/examples/text_labels_and_annotations/legend.py @@ -24,8 +24,8 @@ #Creates an arrow with pre-defined label. ax.annotate("", - xy=(1.5,4.5), xytext=(1.5,9.0), - arrowprops={'arrowstyle':'<->', 'color':'C7'}, label='distance') + xy=(1.5,4.5), xytext=(1.5,9.0), + arrowprops={'arrowstyle': '<->', 'color': 'C7'}, label='distance') legend = ax.legend(loc='upper center', shadow=True, fontsize='x-large') diff --git a/lib/matplotlib/legend_handler.py b/lib/matplotlib/legend_handler.py index 0c6611c60f37..cd7a43ca6ec7 100644 --- a/lib/matplotlib/legend_handler.py +++ b/lib/matplotlib/legend_handler.py @@ -797,4 +797,4 @@ def create_artists( height, fontsize, trans, - ) \ No newline at end of file + ) From e6b746d6ab6aa5b1c859912ba9668dbb42f7e10f Mon Sep 17 00:00:00 2001 From: Wojciech Lange Date: Thu, 14 Feb 2019 14:23:59 +0100 Subject: [PATCH 06/26] flake8 --- examples/text_labels_and_annotations/legend.py | 2 +- lib/matplotlib/legend.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/text_labels_and_annotations/legend.py b/examples/text_labels_and_annotations/legend.py index b9543f2c1eab..c926cfd45588 100644 --- a/examples/text_labels_and_annotations/legend.py +++ b/examples/text_labels_and_annotations/legend.py @@ -24,7 +24,7 @@ #Creates an arrow with pre-defined label. ax.annotate("", - xy=(1.5,4.5), xytext=(1.5,9.0), + xy=(1.5, 4.5), xytext=(1.5, 9.0), arrowprops={'arrowstyle': '<->', 'color': 'C7'}, label='distance') legend = ax.legend(loc='upper center', shadow=True, fontsize='x-large') diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index 58881d72820c..ec55e3a16091 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -32,7 +32,7 @@ from matplotlib.cbook import silent_list, is_hashable, warn_deprecated from matplotlib.font_manager import FontProperties from matplotlib.lines import Line2D -from matplotlib.patches import (Patch, Rectangle, Shadow, FancyBboxPatch, +from matplotlib.patches import (Patch, Rectangle, Shadow, FancyBboxPatch, FancyArrowPatch) from matplotlib.collections import (LineCollection, RegularPolyCollection, CircleCollection, PathCollection, From e3f73d38b467d01c05b6a1919b8d991302397214 Mon Sep 17 00:00:00 2001 From: Wojciech Lange Date: Thu, 14 Feb 2019 15:27:51 +0100 Subject: [PATCH 07/26] flake8 --- examples/text_labels_and_annotations/legend.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/text_labels_and_annotations/legend.py b/examples/text_labels_and_annotations/legend.py index c926cfd45588..f9a7c1460c59 100644 --- a/examples/text_labels_and_annotations/legend.py +++ b/examples/text_labels_and_annotations/legend.py @@ -24,7 +24,7 @@ #Creates an arrow with pre-defined label. ax.annotate("", - xy=(1.5, 4.5), xytext=(1.5, 9.0), + xy=(1.5, 4.5), xytext=(1.5, 9.0), arrowprops={'arrowstyle': '<->', 'color': 'C7'}, label='distance') legend = ax.legend(loc='upper center', shadow=True, fontsize='x-large') From 5aaa29dff457a25420f01a0f8c921f6981fbd9b2 Mon Sep 17 00:00:00 2001 From: Wojciech Lange Date: Thu, 18 Jul 2019 17:21:10 +0200 Subject: [PATCH 08/26] Refering to community suggestions --- .../text_labels_and_annotations/legend.py | 7 ++ lib/matplotlib/legend.py | 13 ++-- lib/matplotlib/legend_handler.py | 64 ++++++++++++++++++- 3 files changed, 79 insertions(+), 5 deletions(-) diff --git a/examples/text_labels_and_annotations/legend.py b/examples/text_labels_and_annotations/legend.py index 7e6162a51c25..3d288e0821e8 100644 --- a/examples/text_labels_and_annotations/legend.py +++ b/examples/text_labels_and_annotations/legend.py @@ -23,6 +23,13 @@ legend = ax.legend(loc='upper center', shadow=True, fontsize='x-large') + +#Creates an arrow with pre-defined label. +ax.annotate("", + xy=(1.5, 4.5), xytext=(1.5, 9.0), + arrowprops={'arrowstyle': '<->', 'color': 'C7'}, label='distance') + + # Put a nicer background color on the legend. legend.get_frame().set_facecolor('C0') diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index fe6d70c8c529..982b81ffceb7 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -32,7 +32,8 @@ from matplotlib.cbook import silent_list, is_hashable, warn_deprecated from matplotlib.font_manager import FontProperties from matplotlib.lines import Line2D -from matplotlib.patches import Patch, Rectangle, Shadow, FancyBboxPatch +from matplotlib.patches import (Patch, Rectangle, Shadow, FancyBboxPatch, + FancyArrowPatch) from matplotlib.collections import (LineCollection, RegularPolyCollection, CircleCollection, PathCollection, PolyCollection) @@ -43,6 +44,7 @@ from matplotlib.offsetbox import DraggableOffsetBox from matplotlib.container import ErrorbarContainer, BarContainer, StemContainer +from matplotlib.text import Annotation, Text from . import legend_handler @@ -678,7 +680,9 @@ def _approx_text_height(self, renderer=None): update_func=legend_handler.update_from_first_child), tuple: legend_handler.HandlerTuple(), PathCollection: legend_handler.HandlerPathCollection(), - PolyCollection: legend_handler.HandlerPolyCollection() + PolyCollection: legend_handler.HandlerPolyCollection(), + FancyArrowPatch: legend_handler.HandlerFancyArrowPatch(), + Annotation: legend_handler.HandlerAnnotation() } # (get|set|update)_default_handler_maps are public interfaces to @@ -865,6 +869,7 @@ def _init_legend_box(self, handles, labels, markerfirst=True): children=[self._legend_title_box, self._legend_handle_box]) self._legend_box.set_figure(self.figure) + self._legend_box.set_offset(self._findoffset) self.texts = text_list self.legendHandles = handle_list @@ -1219,12 +1224,12 @@ def _get_legend_handles(axs, legend_handler_map=None): handles_original = [] for ax in axs: handles_original += (ax.lines + ax.patches + - ax.collections + ax.containers) + ax.collections + ax.containers + ax.texts) # support parasite axes: if hasattr(ax, 'parasites'): for axx in ax.parasites: handles_original += (axx.lines + axx.patches + - axx.collections + axx.containers) + axx.collections + axx.containers + axx.texts) handler_map = Legend.get_default_handler_map() diff --git a/lib/matplotlib/legend_handler.py b/lib/matplotlib/legend_handler.py index 5d4e55a351da..fdbb0f716bcc 100644 --- a/lib/matplotlib/legend_handler.py +++ b/lib/matplotlib/legend_handler.py @@ -29,8 +29,9 @@ def legend_artist(self, legend, orig_handle, fontsize, handlebox) import numpy as np from matplotlib.lines import Line2D -from matplotlib.patches import Rectangle +from matplotlib.patches import Rectangle, FancyArrowPatch import matplotlib.collections as mcoll +from matplotlib.text import Text, Annotation import matplotlib.colors as mcolors @@ -723,3 +724,64 @@ def create_artists(self, legend, orig_handle, self.update_prop(p, orig_handle, legend) p.set_transform(trans) return [p] + +class HandlerFancyArrowPatch(HandlerPatch): + """ + Handler for FancyArrowPatch instances. + """ + def _create_patch(self, legend, orig_handle, xdescent, ydescent, width, + height, fontsize): + arrow = FancyArrowPatch([-xdescent, + -ydescent + height / 2], + [-xdescent + width, + -ydescent + height / 2], + mutation_scale=width / 3) + arrow.set_arrowstyle(orig_handle.get_arrowstyle()) + return arrow + + +class HandlerAnnotation(HandlerBase): + """ + Handler for Annotation instances. + Defers to HandlerFancyArrowPatch to draw the annotation arrow (if any). + Parameters + ---------- + pad : float, optional + If None, fall back to `legend.borderpad` asstr the default. + In units of fraction of font size. + Default is None. + width_ratios : tuple, optional + The relative width of the respective text/arrow legend annotation pair. + Must be of length 2. + Default is [1,4]. + """ + + def __init__( + self, + pad=None, + width_ratios=[1, 4], + **kwargs + ): + + self._pad = pad + self._width_ratios = width_ratios + + HandlerBase.__init__(self, **kwargs) + + def create_artists(self, legend, orig_handle, xdescent, + ydescent, width, height, fontsize, + trans, + ): + + if orig_handle.arrow_patch is not None: + + # Arrow without text + + handler = HandlerFancyArrowPatch() + handle = orig_handle.arrow_patch + + return handler.create_artists(legend, handle, xdescent, + ydescent, width, height, + fontsize, + trans, + ) \ No newline at end of file From 45925b44649e2632059499d55c36067da1f4d85d Mon Sep 17 00:00:00 2001 From: Haloxxx Date: Thu, 18 Jul 2019 19:33:20 +0200 Subject: [PATCH 09/26] Update legend.py --- lib/matplotlib/legend.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index 7122dd31e6b4..fa1d4fed1ae3 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -1230,7 +1230,7 @@ def _get_legend_handles(axs, legend_handler_map=None): if hasattr(ax, 'parasites'): for axx in ax.parasites: handles_original += (axx.lines + axx.patches + - axx.collections + axx.containers + axx.texts) + axx.collections + axx.containers + ax.texts) handler_map = Legend.get_default_handler_map() From cb0b11c4ad7d162e761abfe1edb1b228de1967b8 Mon Sep 17 00:00:00 2001 From: Haloxxx Date: Thu, 18 Jul 2019 20:45:30 +0200 Subject: [PATCH 10/26] Update legend.py --- lib/matplotlib/legend.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index fa1d4fed1ae3..4fa15f26f398 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -682,7 +682,7 @@ def _approx_text_height(self, renderer=None): PathCollection: legend_handler.HandlerPathCollection(), PolyCollection: legend_handler.HandlerPolyCollection(), FancyArrowPatch: legend_handler.HandlerFancyArrowPatch(), - Annotation: legend_handler.HandlerAnnotation() + Annotation: legend_handler.HandlerAnnotation() # (get|set|update)_default_handler_maps are public interfaces to From 84dfdd70585ba2ace412f458e8c213700ab8ee51 Mon Sep 17 00:00:00 2001 From: Haloxxx Date: Thu, 18 Jul 2019 20:47:52 +0200 Subject: [PATCH 11/26] Update legend.py --- lib/matplotlib/legend.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index 4fa15f26f398..a8b2179a7ef2 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -1230,7 +1230,7 @@ def _get_legend_handles(axs, legend_handler_map=None): if hasattr(ax, 'parasites'): for axx in ax.parasites: handles_original += (axx.lines + axx.patches + - axx.collections + axx.containers + ax.texts) + axx.collections + axx.containers + axx.texts) handler_map = Legend.get_default_handler_map() From f121fd4e76e5d0a5f7251e149d205a1b656a628e Mon Sep 17 00:00:00 2001 From: Haloxxx Date: Fri, 19 Jul 2019 00:04:56 +0200 Subject: [PATCH 12/26] Update legend.py --- lib/matplotlib/legend.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index a8b2179a7ef2..2ad8c80593c9 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -683,6 +683,7 @@ def _approx_text_height(self, renderer=None): PolyCollection: legend_handler.HandlerPolyCollection(), FancyArrowPatch: legend_handler.HandlerFancyArrowPatch(), Annotation: legend_handler.HandlerAnnotation() + } # (get|set|update)_default_handler_maps are public interfaces to From b4db694ee991c866fa84756b2eb7af4d1cb8d076 Mon Sep 17 00:00:00 2001 From: Haloxxx Date: Fri, 19 Jul 2019 00:05:52 +0200 Subject: [PATCH 13/26] Update legend_handler.py --- lib/matplotlib/legend_handler.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/matplotlib/legend_handler.py b/lib/matplotlib/legend_handler.py index 061bfd2ba51e..b9437e82e42a 100644 --- a/lib/matplotlib/legend_handler.py +++ b/lib/matplotlib/legend_handler.py @@ -782,6 +782,4 @@ def create_artists(self, legend, orig_handle, xdescent, return handler.create_artists(legend, handle, xdescent, ydescent, width, height, - fontsize, - trans, - ) + fontsize, trans) From 2b944d16d415598e4d3581bb4d59b29656d68910 Mon Sep 17 00:00:00 2001 From: Haloxxx Date: Fri, 19 Jul 2019 13:27:11 +0200 Subject: [PATCH 14/26] Update legend.py --- examples/text_labels_and_annotations/legend.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/examples/text_labels_and_annotations/legend.py b/examples/text_labels_and_annotations/legend.py index fc3a5476c165..77b5159da10e 100644 --- a/examples/text_labels_and_annotations/legend.py +++ b/examples/text_labels_and_annotations/legend.py @@ -31,9 +31,8 @@ #Creates an arrow with pre-defined label. -ax.annotate("", - xy=(1.5, 4.5), xytext=(1.5, 9.0), - arrowprops={'arrowstyle': '<->', 'color': 'C7'}, label='distance') +ax.annotate("", xy=(1.5, 4.5), xytext=(1.5, 9.0), + arrowprops={'arrowstyle': '<->', 'color': 'C7'}, label='distance') # Put a nicer background color on the legend. From 540d0f918fd37ac695809628df377fdc9798b93b Mon Sep 17 00:00:00 2001 From: Haloxxx Date: Fri, 19 Jul 2019 13:30:26 +0200 Subject: [PATCH 15/26] Update legend.py --- lib/matplotlib/legend.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index 2ad8c80593c9..33cd3cea0fde 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -694,7 +694,7 @@ def get_default_handler_map(cls): """ A class method that returns the default handler map. """ - return cls._default_handler_map + return cls._default_handler_map @classmethod def set_default_handler_map(cls, handler_map): @@ -1213,7 +1213,6 @@ def draggable(self, state=None, use_blit=False, update="loc"): return self._draggable - # Helper functions to parse legend arguments for both `figure.legend` and # `axes.legend`: def _get_legend_handles(axs, legend_handler_map=None): From 7e876fe975eceb3dccbd2715cb8b8b28d7a9aa1a Mon Sep 17 00:00:00 2001 From: Haloxxx Date: Fri, 19 Jul 2019 13:35:31 +0200 Subject: [PATCH 16/26] Update legend_handler.py --- lib/matplotlib/legend_handler.py | 43 ++++++++++++++------------------ 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/lib/matplotlib/legend_handler.py b/lib/matplotlib/legend_handler.py index b9437e82e42a..5fac07d4a983 100644 --- a/lib/matplotlib/legend_handler.py +++ b/lib/matplotlib/legend_handler.py @@ -741,34 +741,29 @@ def _create_patch(self, legend, orig_handle, xdescent, ydescent, width, class HandlerAnnotation(HandlerBase): - """ - Handler for Annotation instances. - Defers to HandlerFancyArrowPatch to draw the annotation arrow (if any). - Parameters - ---------- - pad : float, optional - If None, fall back to `legend.borderpad` asstr the default. - In units of fraction of font size. - Default is None. - width_ratios : tuple, optional - The relative width of the respective text/arrow legend annotation pair. - Must be of length 2. - Default is [1,4]. - """ + """ + Handler for Annotation instances. + Defers to HandlerFancyArrowPatch to draw the annotation arrow (if any). + Parameters + ---------- + pad : float, optional + If None, fall back to `legend.borderpad` asstr the default. + In units of fraction of font size. + Default is None. + width_ratios : tuple, optional + The relative width of the respective text/arrow legend annotation pair. + Must be of length 2. + Default is [1,4]. + """ - def __init__( - self, - pad=None, - width_ratios=[1, 4], - **kwargs - ): + def __init__(self, pad=None, width_ratios=[1, 4], **kwargs): - self._pad = pad - self._width_ratios = width_ratios + self._pad = pad + self._width_ratios = width_ratios - HandlerBase.__init__(self, **kwargs) + HandlerBase.__init__(self, **kwargs) - def create_artists(self, legend, orig_handle, xdescent, + def create_artists(self, legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans, ): From 835f79176e3066bf9fe5f022a896f1a81fbcd2a3 Mon Sep 17 00:00:00 2001 From: Wojciech Lange Date: Fri, 2 Oct 2020 13:42:28 +0200 Subject: [PATCH 17/26] fix --- lib/matplotlib/legend.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index 45ead2aeecfe..69bf0a0fc56e 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -34,11 +34,7 @@ from matplotlib.font_manager import FontProperties from matplotlib.lines import Line2D from matplotlib.patches import (Patch, Rectangle, Shadow, FancyBboxPatch, -<<<<<<< HEAD - FancyArrowPatch) -======= - StepPatch) ->>>>>>> ae47ad26eda9c69b182837feaece416c6017d29f + FancyArrowPatch, StepPatch) from matplotlib.collections import (LineCollection, RegularPolyCollection, CircleCollection, PathCollection, PolyCollection) From 3418472526f328de231264f0eaa5a08630e0b574 Mon Sep 17 00:00:00 2001 From: Wojciech Lange Date: Fri, 2 Oct 2020 13:55:03 +0200 Subject: [PATCH 18/26] flake8 --- lib/matplotlib/legend_handler.py | 70 ++++++++++++++++---------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/lib/matplotlib/legend_handler.py b/lib/matplotlib/legend_handler.py index 7df680c791d1..5b86474cb956 100644 --- a/lib/matplotlib/legend_handler.py +++ b/lib/matplotlib/legend_handler.py @@ -770,21 +770,22 @@ def create_artists(self, legend, orig_handle, p.set_transform(trans) return [p] + class HandlerFancyArrowPatch(HandlerPatch): - """ - Handler for FancyArrowPatch instances. - """ - def _create_patch(self, legend, orig_handle, xdescent, ydescent, width, - height, fontsize): - arrow = FancyArrowPatch([-xdescent, - -ydescent + height / 2], - [-xdescent + width, - -ydescent + height / 2], - mutation_scale=width / 3) - arrow.set_arrowstyle(orig_handle.get_arrowstyle()) - return arrow - - + """ + Handler for FancyArrowPatch instances. + """ + def _create_patch(self, legend, orig_handle, xdescent, ydescent, width, + height, fontsize): + arrow = FancyArrowPatch([-xdescent, + -ydescent + height / 2], + [-xdescent + width, + -ydescent + height / 2], + mutation_scale=width / 3) + arrow.set_arrowstyle(orig_handle.get_arrowstyle()) + return arrow + + class HandlerAnnotation(HandlerBase): """ Handler for Annotation instances. @@ -800,26 +801,25 @@ class HandlerAnnotation(HandlerBase): Must be of length 2. Default is [1,4]. """ - + def __init__(self, pad=None, width_ratios=[1, 4], **kwargs): - - self._pad = pad + + self._pad = pad self._width_ratios = width_ratios - - HandlerBase.__init__(self, **kwargs) - - def create_artists(self, legend, orig_handle, xdescent, - ydescent, width, height, fontsize, - trans, - ): - - if orig_handle.arrow_patch is not None: - - # Arrow without text - - handler = HandlerFancyArrowPatch() - handle = orig_handle.arrow_patch - - return handler.create_artists(legend, handle, xdescent, - ydescent, width, height, - fontsize, trans) + + HandlerBase.__init__(self, **kwargs) + + def create_artists(self, legend, orig_handle, xdescent, + ydescent, width, height, fontsize, + trans): + + if orig_handle.arrow_patch is not None: + + # Arrow without text + + handler = HandlerFancyArrowPatch() + handle = orig_handle.arrow_patch + + return handler.create_artists(legend, handle, xdescent, + ydescent, width, height, + fontsize, trans) From bf50d89e7ae9e3588b1a47da017672e17a6e4779 Mon Sep 17 00:00:00 2001 From: Wojciech Lange Date: Fri, 2 Oct 2020 14:05:20 +0200 Subject: [PATCH 19/26] flake8 --- lib/matplotlib/legend.py | 42 ++++------------------------------------ 1 file changed, 4 insertions(+), 38 deletions(-) diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index 69bf0a0fc56e..5ec7d98c14a5 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -635,10 +635,9 @@ def draw(self, renderer): PathCollection: legend_handler.HandlerPathCollection(), PolyCollection: legend_handler.HandlerPolyCollection(), FancyArrowPatch: legend_handler.HandlerFancyArrowPatch(), - Annotation: legend_handler.HandlerAnnotation() + Annotation: legend_handler.HandlerAnnotation() } - # (get|set|update)_default_handler_maps are public interfaces to # modify the default handler map. @@ -647,7 +646,7 @@ def get_default_handler_map(cls): """ A class method that returns the default handler map. """ - return cls._default_handler_map + return cls._default_handler_map @classmethod def set_default_handler_map(cls, handler_map): @@ -1108,39 +1107,6 @@ def get_draggable(self): """Return ``True`` if the legend is draggable, ``False`` otherwise.""" return self._draggable is not None -<<<<<<< HEAD - def draggable(self, state=None, use_blit=False, update="loc"): - """ - Set the draggable state -- if state is - - * None : toggle the current state - - * True : turn draggable on - - * False : turn draggable off - - If draggable is on, you can drag the legend on the canvas with - the mouse. The `.DraggableLegend` helper instance is returned if - draggable is on. - - The update parameter control which parameter of the legend changes - when dragged. If update is "loc", the *loc* parameter of the legend - is changed. If "bbox", the *bbox_to_anchor* parameter is changed. - """ - warn_deprecated("2.2", - message="Legend.draggable() is deprecated in " - "favor of Legend.set_draggable(). " - "Legend.draggable may be reintroduced as a " - "property in future releases.") - - if state is None: - state = not self.get_draggable() # toggle state - - self.set_draggable(state, use_blit, update) - - return self._draggable -======= ->>>>>>> ae47ad26eda9c69b182837feaece416c6017d29f # Helper functions to parse legend arguments for both `figure.legend` and # `axes.legend`: @@ -1159,8 +1125,8 @@ def _get_legend_handles(axs, legend_handler_map=None): if hasattr(ax, 'parasites'): for axx in ax.parasites: handles_original += (axx.lines + axx.patches + - axx.collections + axx.containers + axx.texts) - + axx.collections + axx.containers + + axx.texts) handler_map = Legend.get_default_handler_map() From 12efc367404271dcbbcd2acfa1edd9078c53c472 Mon Sep 17 00:00:00 2001 From: Wojciech Lange Date: Fri, 2 Oct 2020 14:08:39 +0200 Subject: [PATCH 20/26] flake8 --- examples/text_labels_and_annotations/legend.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/examples/text_labels_and_annotations/legend.py b/examples/text_labels_and_annotations/legend.py index 77b5159da10e..03d2fbf35b9b 100644 --- a/examples/text_labels_and_annotations/legend.py +++ b/examples/text_labels_and_annotations/legend.py @@ -21,19 +21,16 @@ ax.plot(a, d, 'k:', label='Data length') ax.plot(a, c + d, 'k', label='Total message length') - #Creates an arrow with pre-defined label. ax.annotate("", xy=(1.5, 4.5), xytext=(1.5, 9.0), arrowprops={'arrowstyle': '<->', 'color': 'C7'}, label='distance') -legend = ax.legend(loc='upper center', shadow=True, fontsize='x-large') +legend = ax.legend(loc='upper center', shadow=True, fontsize='x-large') -#Creates an arrow with pre-defined label. ax.annotate("", xy=(1.5, 4.5), xytext=(1.5, 9.0), - arrowprops={'arrowstyle': '<->', 'color': 'C7'}, label='distance') - + arrowprops={'arrowstyle': '<->', 'color': 'C7'}, label='distance') # Put a nicer background color on the legend. legend.get_frame().set_facecolor('C0') From 19bdd5c8a5dcb21f2624f462152434668761c62c Mon Sep 17 00:00:00 2001 From: Wojciech Lange Date: Fri, 2 Oct 2020 14:11:25 +0200 Subject: [PATCH 21/26] flake8 --- lib/matplotlib/legend.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index 5ec7d98c14a5..38311f6ed254 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -45,7 +45,7 @@ from matplotlib.offsetbox import DraggableOffsetBox from matplotlib.container import ErrorbarContainer, BarContainer, StemContainer -from matplotlib.text import Annotation, Text +from matplotlib.text import Annotation from . import legend_handler From d4bdc20325a74a758e0d2c74cf9b781314b1cbe5 Mon Sep 17 00:00:00 2001 From: Wojciech Lange Date: Fri, 2 Oct 2020 14:14:56 +0200 Subject: [PATCH 22/26] flake8 --- lib/matplotlib/legend_handler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/legend_handler.py b/lib/matplotlib/legend_handler.py index 5b86474cb956..b07a33f5accb 100644 --- a/lib/matplotlib/legend_handler.py +++ b/lib/matplotlib/legend_handler.py @@ -31,7 +31,6 @@ def legend_artist(self, legend, orig_handle, fontsize, handlebox) from matplotlib.lines import Line2D from matplotlib.patches import Rectangle, FancyArrowPatch import matplotlib.collections as mcoll -from matplotlib.text import Text, Annotation import matplotlib.colors as mcolors @@ -787,6 +786,7 @@ def _create_patch(self, legend, orig_handle, xdescent, ydescent, width, class HandlerAnnotation(HandlerBase): + """ Handler for Annotation instances. Defers to HandlerFancyArrowPatch to draw the annotation arrow (if any). From d784f07957de4d25e2a99901a2fc9720841cc393 Mon Sep 17 00:00:00 2001 From: Wojciech Lange Date: Fri, 2 Oct 2020 14:20:00 +0200 Subject: [PATCH 23/26] flake8 --- lib/matplotlib/legend_handler.py | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/matplotlib/legend_handler.py b/lib/matplotlib/legend_handler.py index b07a33f5accb..4a4ee4abecc8 100644 --- a/lib/matplotlib/legend_handler.py +++ b/lib/matplotlib/legend_handler.py @@ -786,7 +786,6 @@ def _create_patch(self, legend, orig_handle, xdescent, ydescent, width, class HandlerAnnotation(HandlerBase): - """ Handler for Annotation instances. Defers to HandlerFancyArrowPatch to draw the annotation arrow (if any). From 2d788f894983b41f0e36b1c54bfd015d55967f1f Mon Sep 17 00:00:00 2001 From: Wojciech Lange Date: Fri, 2 Oct 2020 14:26:34 +0200 Subject: [PATCH 24/26] flake8 --- lib/matplotlib/legend_handler.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/matplotlib/legend_handler.py b/lib/matplotlib/legend_handler.py index 4a4ee4abecc8..b07a33f5accb 100644 --- a/lib/matplotlib/legend_handler.py +++ b/lib/matplotlib/legend_handler.py @@ -786,6 +786,7 @@ def _create_patch(self, legend, orig_handle, xdescent, ydescent, width, class HandlerAnnotation(HandlerBase): + """ Handler for Annotation instances. Defers to HandlerFancyArrowPatch to draw the annotation arrow (if any). From 032433add827b4753ded69652845355ee3630e70 Mon Sep 17 00:00:00 2001 From: Wojciech Lange Date: Fri, 2 Oct 2020 14:29:16 +0200 Subject: [PATCH 25/26] flake8 --- lib/matplotlib/legend_handler.py | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/matplotlib/legend_handler.py b/lib/matplotlib/legend_handler.py index b07a33f5accb..4a4ee4abecc8 100644 --- a/lib/matplotlib/legend_handler.py +++ b/lib/matplotlib/legend_handler.py @@ -786,7 +786,6 @@ def _create_patch(self, legend, orig_handle, xdescent, ydescent, width, class HandlerAnnotation(HandlerBase): - """ Handler for Annotation instances. Defers to HandlerFancyArrowPatch to draw the annotation arrow (if any). From f8088cff8640e51e42490f05bee940e69ef9f605 Mon Sep 17 00:00:00 2001 From: Wojciech Lange Date: Fri, 2 Oct 2020 14:41:06 +0200 Subject: [PATCH 26/26] flake8 --- lib/matplotlib/legend_handler.py | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/matplotlib/legend_handler.py b/lib/matplotlib/legend_handler.py index 4a4ee4abecc8..e64929bfa3be 100644 --- a/lib/matplotlib/legend_handler.py +++ b/lib/matplotlib/legend_handler.py @@ -800,7 +800,6 @@ class HandlerAnnotation(HandlerBase): Must be of length 2. Default is [1,4]. """ - def __init__(self, pad=None, width_ratios=[1, 4], **kwargs): self._pad = pad