Skip to content
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
8 changes: 4 additions & 4 deletions seaborn/_core/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1582,7 +1582,7 @@ def _make_legend(self, p: Plot) -> None:
merged_contents: dict[
tuple[str, str | int], tuple[list[Artist], list[str]],
] = {}
for key, artists, labels in self._legend_contents:
for key, new_artists, labels in self._legend_contents:
# Key is (name, id); we need the id to resolve variable uniqueness,
# but will need the name in the next step to title the legend
if key in merged_contents:
Expand All @@ -1591,11 +1591,11 @@ def _make_legend(self, p: Plot) -> None:
for i, artist in enumerate(existing_artists):
# Matplotlib accepts a tuple of artists and will overlay them
if isinstance(artist, tuple):
artist += artist[i],
artist += new_artists[i],
else:
existing_artists[i] = artist, artists[i]
existing_artists[i] = artist, new_artists[i]
else:
merged_contents[key] = artists.copy(), labels
merged_contents[key] = new_artists.copy(), labels

# TODO explain
loc = "center right" if self._pyplot else "center left"
Expand Down
14 changes: 14 additions & 0 deletions tests/_core/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1963,6 +1963,20 @@ def _legend_artist(self, variables, value, scales):
assert len(contents.findobj(mpl.lines.Line2D)) == len(names)
assert len(contents.findobj(mpl.patches.Patch)) == len(names)

def test_three_layers(self, xy):

class MockMarkLine(MockMark):
def _legend_artist(self, variables, value, scales):
return mpl.lines.Line2D([], [])

s = pd.Series(["a", "b", "a", "c"], name="s")
p = Plot(**xy, color=s)
for _ in range(3):
p = p.add(MockMarkLine())
p = p.plot()
texts = p._figure.legends[0].get_texts()
assert len(texts) == len(s.unique())

def test_identity_scale_ignored(self, xy):

s = pd.Series(["r", "g", "b", "g"])
Expand Down