Skip to content

Commit 2a68c12

Browse files
BUG: weights is not working for multiple columns in df.plot.hist (#33440)
1 parent b5f6e59 commit 2a68c12

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

doc/source/whatsnew/v1.1.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ Plotting
521521
^^^^^^^^
522522

523523
- :func:`.plot` for line/bar now accepts color by dictonary (:issue:`8193`).
524-
-
524+
- Bug in :meth:`DataFrame.plot.hist` where weights are not working for multiple columns (:issue:`33173`)
525525
- Bug in :meth:`DataFrame.boxplot` and :meth:`DataFrame.plot.boxplot` lost color attributes of ``medianprops``, ``whiskerprops``, ``capprops`` and ``medianprops`` (:issue:`30346`)
526526

527527

pandas/plotting/_matplotlib/hist.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ def _args_adjust(self):
2828
values = values[~isna(values)]
2929

3030
_, self.bins = np.histogram(
31-
values,
32-
bins=self.bins,
33-
range=self.kwds.get("range", None),
34-
weights=self.kwds.get("weights", None),
31+
values, bins=self.bins, range=self.kwds.get("range", None)
3532
)
3633

3734
if is_list_like(self.bottom):
@@ -77,6 +74,14 @@ def _make_plot(self):
7774
kwds["style"] = style
7875

7976
kwds = self._make_plot_keywords(kwds, y)
77+
78+
# We allow weights to be a multi-dimensional array, e.g. a (10, 2) array,
79+
# and each sub-array (10,) will be called in each iteration. If users only
80+
# provide 1D array, we assume the same weights is used for all iterations
81+
weights = kwds.get("weights", None)
82+
if weights is not None and np.ndim(weights) != 1:
83+
kwds["weights"] = weights[:, i]
84+
8085
artists = self._plot(ax, y, column_num=i, stacking_id=stacking_id, **kwds)
8186
self._add_legend_handle(artists[0], label, index=i)
8287

pandas/tests/plotting/test_frame.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,25 @@ def test_hist_df(self):
16821682
axes = df.plot.hist(rot=50, fontsize=8, orientation="horizontal")
16831683
self._check_ticks_props(axes, xrot=0, yrot=50, ylabelsize=8)
16841684

1685+
@pytest.mark.parametrize(
1686+
"weights", [0.1 * np.ones(shape=(100,)), 0.1 * np.ones(shape=(100, 2))]
1687+
)
1688+
def test_hist_weights(self, weights):
1689+
# GH 33173
1690+
np.random.seed(0)
1691+
df = pd.DataFrame(dict(zip(["A", "B"], np.random.randn(2, 100,))))
1692+
1693+
ax1 = _check_plot_works(df.plot, kind="hist", weights=weights)
1694+
ax2 = _check_plot_works(df.plot, kind="hist")
1695+
1696+
patch_height_with_weights = [patch.get_height() for patch in ax1.patches]
1697+
1698+
# original heights with no weights, and we manually multiply with example
1699+
# weights, so after multiplication, they should be almost same
1700+
expected_patch_height = [0.1 * patch.get_height() for patch in ax2.patches]
1701+
1702+
tm.assert_almost_equal(patch_height_with_weights, expected_patch_height)
1703+
16851704
def _check_box_coord(
16861705
self,
16871706
patches,

0 commit comments

Comments
 (0)