Skip to content
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

Add label parameter to pointplot #3016

Merged
merged 4 commits into from
Oct 12, 2022
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
4 changes: 3 additions & 1 deletion doc/whatsnew/v0.12.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ v0.12.1 (Unreleased)

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

- |Fix| Make :class:`objects.PolyFit` robust to missing data (:pr:`3010`).
- |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`).

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

- |Fix| Make :class:`objects.PolyFit` robust to missing data (:pr:`3010`).

- |Fix| Fixed a regression in :func:`kdeplot` where passing `cmap` for an unfilled bivariate plot would raise an exception (:pr:`3065`).

- |Build| Seaborn no longer contains doctest-style examples, simplifying the testing infrastructure (:pr:`3034`).
11 changes: 7 additions & 4 deletions seaborn/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1596,7 +1596,7 @@ class _PointPlotter(_CategoricalStatPlotter):
def __init__(self, x, y, hue, data, order, hue_order,
estimator, errorbar, n_boot, units, seed,
markers, linestyles, dodge, join, scale,
orient, color, palette, errwidth=None, capsize=None):
orient, color, palette, errwidth, capsize, label):
"""Initialize the plotter."""
self.establish_variables(x, y, hue, data, orient,
order, hue_order, units)
Expand Down Expand Up @@ -1631,6 +1631,7 @@ def __init__(self, x, y, hue, data, order, hue_order,
self.scale = scale
self.errwidth = errwidth
self.capsize = capsize
self.label = label

@property
def hue_offsets(self):
Expand Down Expand Up @@ -1678,7 +1679,7 @@ def draw_points(self, ax):
x, y = pointpos, self.statistic
ax.scatter(x, y,
linewidth=mew, marker=marker, s=markersize,
facecolor=colors, edgecolor=colors)
facecolor=colors, edgecolor=colors, label=self.label)

else:

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

errorbar = utils._deprecate_ci(errorbar, ci)

plotter = _PointPlotter(x, y, hue, data, order, hue_order,
estimator, errorbar, n_boot, units, seed,
markers, linestyles, dodge, join, scale,
orient, color, palette, errwidth, capsize)
orient, color, palette, errwidth, capsize, label)

if ax is None:
ax = plt.gca()
Expand Down Expand Up @@ -2893,6 +2894,8 @@ def pointplot(
{palette}
{errwidth}
{capsize}
label : string, optional
Label to represent the plot in a legend, only relevant when not using `hue`.
{ax_in}

Returns
Expand Down
18 changes: 16 additions & 2 deletions tests/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@

from seaborn.external.version import Version
from seaborn._oldcore import categorical_order
from seaborn.axisgrid import FacetGrid
from seaborn.categorical import (
_CategoricalPlotterNew,
Beeswarm,
catplot,
pointplot,
stripplot,
swarmplot,
)
Expand Down Expand Up @@ -2474,8 +2476,10 @@ class TestPointPlotter(CategoricalFixture):
n_boot=100, units=None, seed=None,
order=None, hue_order=None,
markers="o", linestyles="-", dodge=0,
join=True, scale=1,
orient=None, color=None, palette=None,
join=True, scale=1, orient=None,
color=None, palette=None,
errwidth=None, capsize=None, label=None,

)

def test_different_defualt_colors(self):
Expand Down Expand Up @@ -2745,6 +2749,16 @@ def test_errorbar(self, long_df):
expected = mean - 2 * sd, mean + 2 * sd
assert_array_equal(line.get_ydata(), expected)

def test_on_facetgrid(self, long_df):

g = FacetGrid(long_df, hue="a")
g.map(pointplot, "a", "y")
g.add_legend()

order = categorical_order(long_df["a"])
legend_texts = [t.get_text() for t in g.legend.texts]
assert legend_texts == order


class TestCountPlot(CategoricalFixture):

Expand Down