Skip to content

Commit 85c8ed6

Browse files
authored
Add label parameter to pointplot (#3016)
* Add label parameter to pointplot Unbreaks FacetGrid + pointplot (fixes #3004) and is generally useful. * Update default kws in pointplot tests * Update release notes
1 parent 54cab15 commit 85c8ed6

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

doc/whatsnew/v0.12.1.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ v0.12.1 (Unreleased)
1212

1313
- |Enhancement| Marks that sort along the orient axis (e.g. :class:`Line`) now use a stable algorithm (:pr:`3064`).
1414

15-
- |Fix| Make :class:`objects.PolyFit` robust to missing data (:pr:`3010`).
15+
- |Enhancement| |Fix| Add a `label` parameter to :func:`pointplot`, which addresses a regression in 0.12.0 when :func:`pointplot` is passed to :class:`FacetGrid` (:pr:`3016`).
1616

1717
- |Fix| Fixed a bug that caused an exception when more than two layers with the same mappings were added (:pr:`3055`).
1818

19+
- |Fix| Make :class:`objects.PolyFit` robust to missing data (:pr:`3010`).
20+
1921
- |Fix| Fixed a regression in :func:`kdeplot` where passing `cmap` for an unfilled bivariate plot would raise an exception (:pr:`3065`).
2022

2123
- |Build| Seaborn no longer contains doctest-style examples, simplifying the testing infrastructure (:pr:`3034`).

seaborn/categorical.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,7 +1596,7 @@ class _PointPlotter(_CategoricalStatPlotter):
15961596
def __init__(self, x, y, hue, data, order, hue_order,
15971597
estimator, errorbar, n_boot, units, seed,
15981598
markers, linestyles, dodge, join, scale,
1599-
orient, color, palette, errwidth=None, capsize=None):
1599+
orient, color, palette, errwidth, capsize, label):
16001600
"""Initialize the plotter."""
16011601
self.establish_variables(x, y, hue, data, orient,
16021602
order, hue_order, units)
@@ -1631,6 +1631,7 @@ def __init__(self, x, y, hue, data, order, hue_order,
16311631
self.scale = scale
16321632
self.errwidth = errwidth
16331633
self.capsize = capsize
1634+
self.label = label
16341635

16351636
@property
16361637
def hue_offsets(self):
@@ -1678,7 +1679,7 @@ def draw_points(self, ax):
16781679
x, y = pointpos, self.statistic
16791680
ax.scatter(x, y,
16801681
linewidth=mew, marker=marker, s=markersize,
1681-
facecolor=colors, edgecolor=colors)
1682+
facecolor=colors, edgecolor=colors, label=self.label)
16821683

16831684
else:
16841685

@@ -2829,15 +2830,15 @@ def pointplot(
28292830
estimator="mean", errorbar=("ci", 95), n_boot=1000, units=None, seed=None,
28302831
markers="o", linestyles="-", dodge=False, join=True, scale=1,
28312832
orient=None, color=None, palette=None, errwidth=None, ci="deprecated",
2832-
capsize=None, ax=None,
2833+
capsize=None, label=None, ax=None,
28332834
):
28342835

28352836
errorbar = utils._deprecate_ci(errorbar, ci)
28362837

28372838
plotter = _PointPlotter(x, y, hue, data, order, hue_order,
28382839
estimator, errorbar, n_boot, units, seed,
28392840
markers, linestyles, dodge, join, scale,
2840-
orient, color, palette, errwidth, capsize)
2841+
orient, color, palette, errwidth, capsize, label)
28412842

28422843
if ax is None:
28432844
ax = plt.gca()
@@ -2893,6 +2894,8 @@ def pointplot(
28932894
{palette}
28942895
{errwidth}
28952896
{capsize}
2897+
label : string, optional
2898+
Label to represent the plot in a legend, only relevant when not using `hue`.
28962899
{ax_in}
28972900
28982901
Returns

tests/test_categorical.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121

2222
from seaborn.external.version import Version
2323
from seaborn._oldcore import categorical_order
24+
from seaborn.axisgrid import FacetGrid
2425
from seaborn.categorical import (
2526
_CategoricalPlotterNew,
2627
Beeswarm,
2728
catplot,
29+
pointplot,
2830
stripplot,
2931
swarmplot,
3032
)
@@ -2474,8 +2476,10 @@ class TestPointPlotter(CategoricalFixture):
24742476
n_boot=100, units=None, seed=None,
24752477
order=None, hue_order=None,
24762478
markers="o", linestyles="-", dodge=0,
2477-
join=True, scale=1,
2478-
orient=None, color=None, palette=None,
2479+
join=True, scale=1, orient=None,
2480+
color=None, palette=None,
2481+
errwidth=None, capsize=None, label=None,
2482+
24792483
)
24802484

24812485
def test_different_defualt_colors(self):
@@ -2745,6 +2749,16 @@ def test_errorbar(self, long_df):
27452749
expected = mean - 2 * sd, mean + 2 * sd
27462750
assert_array_equal(line.get_ydata(), expected)
27472751

2752+
def test_on_facetgrid(self, long_df):
2753+
2754+
g = FacetGrid(long_df, hue="a")
2755+
g.map(pointplot, "a", "y")
2756+
g.add_legend()
2757+
2758+
order = categorical_order(long_df["a"])
2759+
legend_texts = [t.get_text() for t in g.legend.texts]
2760+
assert legend_texts == order
2761+
27482762

27492763
class TestCountPlot(CategoricalFixture):
27502764

0 commit comments

Comments
 (0)