Skip to content
Prev Previous commit
Next Next commit
Allow marks to be withheld from legend
  • Loading branch information
mwaskom committed Oct 1, 2022
commit 0d16cb482ccc6491a510ed0f8d9a03435eeae292
9 changes: 6 additions & 3 deletions seaborn/_core/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1562,12 +1562,15 @@ def _update_legend_contents(
schema.append(entry)

# Second pass, generate an artist corresponding to each value
contents = []
contents: list[tuple[tuple[str, str | int], Any, list[str]]] = []
for key, variables, (values, labels) in schema:
artists = []
for val in values:
artists.append(mark._legend_artist(variables, val, scales))
contents.append((key, artists, labels))
artist = mark._legend_artist(variables, val, scales)
if artist is not None:
artists.append(artist)
if contents:
contents.append((key, artists, labels))

self._legend_contents.extend(contents)

Expand Down
4 changes: 2 additions & 2 deletions seaborn/_marks/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ def _plot(
def _legend_artist(
self, variables: list[str], value: Any, scales: dict[str, Scale],
) -> Artist:
# TODO return some sensible default?
raise NotImplementedError

return None


def resolve_properties(
Expand Down
11 changes: 0 additions & 11 deletions seaborn/_marks/text.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from __future__ import annotations
from collections import defaultdict
from dataclasses import dataclass
from typing import Any

import numpy as np
import matplotlib as mpl
from matplotlib.artist import Artist

from seaborn._marks.base import (
Mark,
Expand All @@ -17,7 +15,6 @@
resolve_color,
document_properties,
)
from seaborn._core.scales import Scale


@document_properties
Expand All @@ -31,7 +28,6 @@ class Text(Mark):
.. include:: ../docstrings/objects.Text.rst

"""

text: MappableString = Mappable("")
color: MappableColor = Mappable("k")
alpha: MappableFloat = Mappable(1)
Expand Down Expand Up @@ -62,10 +58,3 @@ def _plot(self, split_gen, scales, orient):

for ax, ax_vals in ax_data.items():
ax.update_datalim(np.array(ax_vals))

def _legend_artist(
self, variables: list[str], value: Any, scales=dict[str, Scale],
) -> Artist:

# TODO
return mpl.lines.Line2D([], [])
11 changes: 10 additions & 1 deletion tests/_core/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1981,8 +1981,17 @@ def test_anonymous_title(self, xy):
legend, = p._figure.legends
assert legend.get_title().get_text() == ""

def test_legendless_mark(self, xy):

class TestHelpers:
class NoLegendMark(MockMark):
def _legend_artist(self, variables, value, scales):
return None

p = Plot(**xy, color=["a", "b", "c", "d"]).add(NoLegendMark()).plot()
assert not p._figure.legends


class TestDefaultObject:

def test_default_repr(self):

Expand Down