Skip to content

Fix legend color comparisions #10031

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 20, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions lib/matplotlib/legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.

Expand All @@ -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():
if (colors.to_rgba_array(f_h.get_color()) !=
colors.to_rgba_array(h.get_color())).any():
continue
except AttributeError:
pass
try:
if f_h.get_facecolor() != 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 f_h.get_edgecolor() != h.get_edgecolor():
if (colors.to_rgba_array(f_h.get_edgecolor()) !=
colors.to_rgba_array(h.get_edgecolor())).any():
continue
except AttributeError:
pass
Expand All @@ -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
Expand Down