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 percent-based normalization in histplot #2461

Merged
merged 2 commits into from
Feb 1, 2021
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
2 changes: 2 additions & 0 deletions doc/releases/v0.12.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ v0.12.0 (Unreleased)

- In :func:`swarmplot`, the proportion of points that must overlap before issuing a warning can now be controlled with the `warn_thresh` parameter (:pr:`2447`).

- |Enhancement| In :func:`histplot`, added `stat="percent"` as an option for normalization such that bar heights sum to 100 (:pr:`2461`).

- |Fix| In :func:`lineplot, allowed the `dashes` keyword to set the style of a line without mapping a `style` variable (:pr:`2449`).

- |Fix| In :func:`rugplot`, fixed a bug that prevented the use of datetime data (:pr:`2458`).
Expand Down
9 changes: 7 additions & 2 deletions seaborn/_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def __init__(

Parameters
----------
stat : {"count", "frequency", "density", "probability"}
stat : {"count", "frequency", "density", "probability", "percent"}
Aggregate statistic to compute in each bin.

- ``count`` shows the number of observations
Expand All @@ -234,7 +234,8 @@ def __init__(
If True, return the cumulative statistic.

"""
_check_argument("stat", ["count", "density", "probability", "frequency"], stat)
stat_choices = ["count", "frequency", "density", "probability", "percent"]
_check_argument("stat", stat_choices, stat)

self.stat = stat
self.bins = bins
Expand Down Expand Up @@ -335,6 +336,8 @@ def _eval_bivariate(self, x1, x2, weights):

if self.stat == "probability":
hist = hist.astype(float) / hist.sum()
elif self.stat == "percent":
hist = hist.astype(float) / hist.sum() * 100
elif self.stat == "frequency":
hist = hist.astype(float) / area

Expand All @@ -359,6 +362,8 @@ def _eval_univariate(self, x, weights):

if self.stat == "probability":
hist = hist.astype(float) / hist.sum()
elif self.stat == "percent":
hist = hist.astype(float) / hist.sum() * 100
elif self.stat == "frequency":
hist = hist.astype(float) / np.diff(bin_edges)

Expand Down
17 changes: 17 additions & 0 deletions seaborn/tests/test_distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,12 @@ def test_probability_stat_unique_norm(self, long_df):
bar_heights = [b.get_height() for b in bars]
assert sum(bar_heights) == pytest.approx(1)

def test_percent_stat(self, flat_series):

ax = histplot(flat_series, stat="percent")
bar_heights = [b.get_height() for b in ax.patches]
assert sum(bar_heights) == 100

def test_common_bins(self, long_df):

n = 10
Expand Down Expand Up @@ -1810,6 +1816,17 @@ def test_mesh_unique_norm(self, long_df):
density, (x_edges, y_edges) = sub_hist(sub_df["x"], sub_df["y"])
assert_array_equal(mesh_data.data, density.T.flat)

@pytest.mark.parametrize("stat", ["probability", "percent"])
def test_mesh_normalization(self, long_df, stat):

ax = histplot(
long_df, x="x", y="y", stat=stat,
)

mesh_data = ax.collections[0].get_array()
expected_sum = {"probability": 1, "percent": 100}[stat]
assert mesh_data.data.sum() == expected_sum

def test_mesh_colors(self, long_df):

color = "r"
Expand Down