Skip to content

Commit cbbff78

Browse files
committed
BUG: groupby.hist legend should use group keys
1 parent d1cba24 commit cbbff78

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

pandas/plotting/_matplotlib/hist.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ def hist_series(
292292
yrot=None,
293293
figsize=None,
294294
bins=10,
295+
legend=False,
295296
**kwds,
296297
):
297298
import matplotlib.pyplot as plt
@@ -310,8 +311,11 @@ def hist_series(
310311
elif ax.get_figure() != fig:
311312
raise AssertionError("passed axis not bound to passed figure")
312313
values = self.dropna().values
313-
314+
if legend and "label" not in kwds:
315+
kwds["label"] = self.name
314316
ax.hist(values, bins=bins, **kwds)
317+
if legend:
318+
ax.legend()
315319
ax.grid(grid)
316320
axes = np.array([ax])
317321

@@ -360,6 +364,7 @@ def hist_frame(
360364
figsize=None,
361365
layout=None,
362366
bins=10,
367+
legend=False,
363368
**kwds,
364369
):
365370
if by is not None:
@@ -405,9 +410,14 @@ def hist_frame(
405410

406411
for i, col in enumerate(com.try_sort(data.columns)):
407412
ax = _axes[i]
413+
if legend and "label" not in kwds:
414+
kwds["label"] = col
408415
ax.hist(data[col].dropna().values, bins=bins, **kwds)
409416
ax.set_title(col)
410417
ax.grid(grid)
418+
if legend:
419+
ax.legend()
420+
kwds.pop("label")
411421

412422
_set_ticks_props(
413423
axes, xlabelsize=xlabelsize, xrot=xrot, ylabelsize=ylabelsize, yrot=yrot

0 commit comments

Comments
 (0)