From 90473b7c160813253e11c302abf105e3ea35ec34 Mon Sep 17 00:00:00 2001 From: Michael Waskom Date: Tue, 14 Jun 2022 20:48:48 -0400 Subject: [PATCH 1/3] Add width parameter to barplot --- doc/releases/v0.12.0.txt | 2 ++ seaborn/categorical.py | 10 ++++++---- tests/test_categorical.py | 28 ++++++++++++++-------------- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/doc/releases/v0.12.0.txt b/doc/releases/v0.12.0.txt index 77b3b5b62b..4711ecaa88 100644 --- a/doc/releases/v0.12.0.txt +++ b/doc/releases/v0.12.0.txt @@ -51,6 +51,8 @@ Other updates - |Enhancement| Example datasets are now stored in an OS-specific cache location (as determined by `appdirs`) rather than in the user's home directory. Users should feel free to remove `~/seaborn-data` if desired (:pr:`2773`). +- |Enhancement| Added a `width` parameter to :func:`barplot` (:pr:`2860`). + - |Enhancement| Error bars in :func:`regplot` now inherit the alpha value of the points they correspond to (:pr:`2540`). - |Enhancement| When using :func:`pairplot` with `corner=True` and `diag_kind=None`, the top left y axis label is no longer hidden (:pr:2850`). diff --git a/seaborn/categorical.py b/seaborn/categorical.py index d8d2b58102..3e24eddaed 100644 --- a/seaborn/categorical.py +++ b/seaborn/categorical.py @@ -1576,8 +1576,8 @@ class _BarPlotter(_CategoricalStatPlotter): def __init__(self, x, y, hue, data, order, hue_order, estimator, ci, n_boot, units, seed, - orient, color, palette, saturation, errcolor, - errwidth, capsize, dodge): + orient, color, palette, saturation, width, + errcolor, errwidth, capsize, dodge): """Initialize the plotter.""" self.establish_variables(x, y, hue, data, orient, order, hue_order, units) @@ -1585,6 +1585,7 @@ def __init__(self, x, y, hue, data, order, hue_order, self.estimate_statistic(estimator, ci, n_boot, seed) self.dodge = dodge + self.width = width self.errcolor = errcolor self.errwidth = errwidth @@ -2743,7 +2744,7 @@ def swarmplot( def barplot( data=None, *, x=None, y=None, hue=None, order=None, hue_order=None, estimator=np.mean, ci=95, n_boot=1000, units=None, seed=None, - orient=None, color=None, palette=None, saturation=.75, + orient=None, color=None, palette=None, saturation=.75, width=.8, errcolor=".26", errwidth=None, capsize=None, dodge=True, ax=None, **kwargs, @@ -2752,7 +2753,7 @@ def barplot( plotter = _BarPlotter(x, y, hue, data, order, hue_order, estimator, ci, n_boot, units, seed, orient, color, palette, saturation, - errcolor, errwidth, capsize, dodge) + width, errcolor, errwidth, capsize, dodge) if ax is None: ax = plt.gca() @@ -2793,6 +2794,7 @@ def barplot( {color} {palette} {saturation} + {width} errcolor : matplotlib color Color used for the error bar lines. {errwidth} diff --git a/tests/test_categorical.py b/tests/test_categorical.py index fac4567299..ebdf9cc1c5 100644 --- a/tests/test_categorical.py +++ b/tests/test_categorical.py @@ -2209,20 +2209,20 @@ class TestBarPlotter(CategoricalFixture): def test_nested_width(self): - kws = self.default_kws.copy() - - p = cat._BarPlotter(**kws) - p.establish_variables("g", "y", hue="h", data=self.df) - assert p.nested_width == .8 / 2 - - p = cat._BarPlotter(**kws) - p.establish_variables("h", "y", "g", data=self.df) - assert p.nested_width == .8 / 3 - - kws["dodge"] = False - p = cat._BarPlotter(**kws) - p.establish_variables("h", "y", "g", data=self.df) - assert p.nested_width == .8 + ax = cat.barplot(data=self.df, x="g", y="y", hue="h") + for bar in ax.patches: + assert bar.get_width() == pytest.approx(.8 / 2) + ax.clear() + + ax = cat.barplot(data=self.df, x="g", y="y", hue="g", width=.5) + for bar in ax.patches: + assert bar.get_width() == pytest.approx(.5 / 3) + ax.clear() + + ax = cat.barplot(data=self.df, x="g", y="y", hue="g", dodge=False) + for bar in ax.patches: + assert bar.get_width() == pytest.approx(.8) + ax.clear() def test_draw_vertical_bars(self): From 4b431aa49977d16fe1aa60c7dbde35d92645dbf9 Mon Sep 17 00:00:00 2001 From: Michael Waskom Date: Tue, 14 Jun 2022 22:36:13 -0400 Subject: [PATCH 2/3] Add width to countplot as well --- seaborn/categorical.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/seaborn/categorical.py b/seaborn/categorical.py index 3e24eddaed..3b69c855f7 100644 --- a/seaborn/categorical.py +++ b/seaborn/categorical.py @@ -2912,7 +2912,7 @@ def pointplot( def countplot( data=None, *, x=None, y=None, hue=None, order=None, hue_order=None, - orient=None, color=None, palette=None, saturation=.75, + orient=None, color=None, palette=None, saturation=.75, width=.8, dodge=True, ax=None, **kwargs ): @@ -2938,7 +2938,7 @@ def countplot( x, y, hue, data, order, hue_order, estimator, ci, n_boot, units, seed, orient, color, palette, saturation, - errcolor, errwidth, capsize, dodge + width, errcolor, errwidth, capsize, dodge ) plotter.value_label = "count" From 6136338c78540b4e6f521edafea6113315db074b Mon Sep 17 00:00:00 2001 From: Michael Waskom Date: Tue, 14 Jun 2022 22:47:08 -0400 Subject: [PATCH 3/3] Update barplot test fixture --- tests/test_categorical.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_categorical.py b/tests/test_categorical.py index ebdf9cc1c5..70554b3c81 100644 --- a/tests/test_categorical.py +++ b/tests/test_categorical.py @@ -2203,7 +2203,8 @@ class TestBarPlotter(CategoricalFixture): estimator=np.mean, ci=95, n_boot=100, units=None, seed=None, order=None, hue_order=None, orient=None, color=None, palette=None, - saturation=.75, errcolor=".26", errwidth=None, + saturation=.75, width=0.8, + errcolor=".26", errwidth=None, capsize=None, dodge=True )