From 415cd6a314385ff8e1407bf9301312268168bfb2 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Sun, 17 Dec 2017 14:07:03 +0300 Subject: [PATCH 1/3] Fix using .get_color() and friends in labels handling This code was introduced in v2.1.1 and, apparently, wasn't tested, because .get_color() returns array. Here is an issue example: https://github.com/sympy/sympy/issues/13730#issuecomment-351027060 --- lib/matplotlib/legend.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index 8c2aaa9b0c16..430aeb741897 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -1350,17 +1350,17 @@ def _in_handles(h, l): if type(f_h) != type(h): continue try: - if f_h.get_color() != h.get_color(): + if (f_h.get_color() != h.get_color()).any(): continue except AttributeError: pass try: - if f_h.get_facecolor() != h.get_facecolor(): + if (f_h.get_facecolor() != h.get_facecolor()).any(): continue except AttributeError: pass try: - if f_h.get_edgecolor() != h.get_edgecolor(): + if (f_h.get_edgecolor() != h.get_edgecolor()).any(): continue except AttributeError: pass From 58a221d9ba4ccf41ed44b80d7502e17bd1a25597 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Sun, 17 Dec 2017 12:18:56 +0000 Subject: [PATCH 2/3] Fix legend color handling --- lib/matplotlib/legend.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index 430aeb741897..555e8f1ccdf0 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -37,6 +37,7 @@ from matplotlib import docstring from matplotlib.artist import Artist, allow_rasterization from matplotlib.cbook import silent_list, is_hashable +import matplotlib.colors as colors from matplotlib.font_manager import FontProperties from matplotlib.lines import Line2D from matplotlib.patches import Patch, Rectangle, Shadow, FancyBboxPatch @@ -1339,7 +1340,7 @@ def _get_legend_handles_labels(axs, legend_handler_map=None): labels = [] def _in_handles(h, l): - # Method to check if we already have a given handle and label. + # Method to check if we already have a given handle and label. # Consider two handles to be the same if they share a label, # color, facecolor, and edgecolor. @@ -1350,17 +1351,20 @@ def _in_handles(h, l): if type(f_h) != type(h): continue try: - if (f_h.get_color() != h.get_color()).any(): + if (colors.to_rgba(f_h.get_color()) != + colors.to_rgba(h.get_color())): continue except AttributeError: pass try: - if (f_h.get_facecolor() != h.get_facecolor()).any(): + if (colors.to_rgba(f_h.get_facecolor()) != + colors.to_rgba(h.get_facecolor())): continue except AttributeError: pass try: - if (f_h.get_edgecolor() != h.get_edgecolor()).any(): + if (colors.to_rgba(f_h.get_edgecolor()) != + colors.to_rgba(h.get_edgecolor())): continue except AttributeError: pass @@ -1369,9 +1373,9 @@ def _in_handles(h, l): for handle in _get_legend_handles(axs, legend_handler_map): label = handle.get_label() - if (label - and not label.startswith('_') - and not _in_handles(handle, label)): + if (label and + not label.startswith('_') and + not _in_handles(handle, label)): handles.append(handle) labels.append(label) return handles, labels From df2e51af44c668f396d218455f1c831dd9fa74f5 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Sun, 17 Dec 2017 15:54:48 +0000 Subject: [PATCH 3/3] Make comparison work with arrays --- lib/matplotlib/legend.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index 555e8f1ccdf0..80674554010f 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -1351,20 +1351,20 @@ def _in_handles(h, l): if type(f_h) != type(h): continue try: - if (colors.to_rgba(f_h.get_color()) != - colors.to_rgba(h.get_color())): + if (colors.to_rgba_array(f_h.get_color()) != + colors.to_rgba_array(h.get_color())).any(): continue except AttributeError: pass try: - if (colors.to_rgba(f_h.get_facecolor()) != - colors.to_rgba(h.get_facecolor())): + if (colors.to_rgba_array(f_h.get_facecolor()) != + colors.to_rgba_array(h.get_facecolor())).any(): continue except AttributeError: pass try: - if (colors.to_rgba(f_h.get_edgecolor()) != - colors.to_rgba(h.get_edgecolor())): + if (colors.to_rgba_array(f_h.get_edgecolor()) != + colors.to_rgba_array(h.get_edgecolor())).any(): continue except AttributeError: pass