Skip to content

FIX: remove repeated label legend logic #10064

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 1 commit into from
Dec 21, 2017
Merged
Show file tree
Hide file tree
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
23 changes: 23 additions & 0 deletions doc/api/api_changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,29 @@ out what caused the breakage and how to fix it by updating your code.
For new features that were added to Matplotlib, please see
:ref:`whats-new`.

API Changes in 2.1.2
====================

`Figure.legend` no longer checks for repeated lines to ignore
-------------------------------------------------------------

`matplotlib.Figure.legend` used to check if a line had the
same label as an existing legend entry. If it also had the same line color
or marker color legend didn't add a new entry for that line. However, the
list of conditions was incomplete, didn't handle RGB tupples,
didn't handle linewidths or linestyles etc.

This logic did not exist in `Axes.legend`. It was included (erroneously)
in Matplotlib 2.1.1 when the legend argument parsing was unified
[#9324](https://github.com/matplotlib/matplotlib/pull/9324). This change
removes that check in `Axes.legend` again to restore the old behavior.

This logic has also been dropped from `.Figure.legend`, where it
was previously undocumented. Repeated
lines with the same label will now each have an entry in the legend. If
you do not want the duplicate entries, don't add a label to the line, or
prepend the label with an underscore.

API Changes in 2.1.1
====================

Expand Down
36 changes: 1 addition & 35 deletions lib/matplotlib/legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1346,43 +1346,9 @@ def _get_legend_handles_labels(axs, legend_handler_map=None):
handles = []
labels = []

def _in_handles(h, l):
# 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.

# Loop through each handle and label already collected
for f_h, f_l in zip(handles, labels):
if f_l != l:
continue
if type(f_h) != type(h):
continue
try:
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_array(f_h.get_facecolor()) !=
colors.to_rgba_array(h.get_facecolor())).any():
continue
except AttributeError:
pass
try:
if (colors.to_rgba_array(f_h.get_edgecolor()) !=
colors.to_rgba_array(h.get_edgecolor())).any():
continue
except AttributeError:
pass
return True
return False

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('_')):
handles.append(handle)
labels.append(label)
return handles, labels
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def test_figure_legend():
axes[0].plot([0, 1], [0, 1], label='y', color='r')
axes[0].plot([0, 1], [0.5, 0.5], label='y', color='k')

axes[1].plot([0, 1], [1, 0], label='y', color='r')
axes[1].plot([0, 1], [1, 0], label='_y', color='r')
axes[1].plot([0, 1], [0, 1], label='z', color='b')
fig.legend()

Expand Down
16 changes: 16 additions & 0 deletions lib/matplotlib/tests/test_legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import matplotlib.transforms as mtransforms
import matplotlib.collections as mcollections
from matplotlib.legend_handler import HandlerTuple
import matplotlib.legend as mlegend
import inspect


Expand Down Expand Up @@ -423,6 +424,21 @@ def test_nanscatter():
ax.grid(True)


def test_legend_repeatcheckok():
fig, ax = plt.subplots()
ax.scatter(0.0, 1.0, color='k', marker='o', label='test')
ax.scatter(0.5, 0.0, color='r', marker='v', label='test')
hl = ax.legend()
hand, lab = mlegend._get_legend_handles_labels([ax])
assert len(lab) == 2
fig, ax = plt.subplots()
ax.scatter(0.0, 1.0, color='k', marker='o', label='test')
ax.scatter(0.5, 0.0, color='k', marker='v', label='test')
hl = ax.legend()
hand, lab = mlegend._get_legend_handles_labels([ax])
assert len(lab) == 2


@image_comparison(baseline_images=['not_covering_scatter'], extensions=['png'])
def test_not_covering_scatter():
colors = ['b', 'g', 'r']
Expand Down