Skip to content

Commit 7bbe3b7

Browse files
authored
Handle single color for multiple datasets in hist (matplotlib#30867)
1 parent 15697ea commit 7bbe3b7

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
``hist()`` supports a single color for multiple datasets
2+
--------------------------------------------------------
3+
4+
It is now possible to pass a single *color* value to `~.Axes.hist()`. This
5+
value is applied to all datasets.

lib/matplotlib/axes/_axes.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7326,6 +7326,9 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
73267326
Color or sequence of colors, one per dataset. Default (``None``)
73277327
uses the standard line color sequence.
73287328
7329+
.. versionadded:: 3.10
7330+
It is now possible to use a single color with multiple datasets.
7331+
73297332
label : str or list of str, optional
73307333
String, or sequence of strings to match multiple datasets. Bar
73317334
charts yield multiple patches per dataset, but only the first gets
@@ -7447,6 +7450,8 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
74477450
if color is None:
74487451
colors = [self._get_lines.get_next_color() for i in range(nx)]
74497452
else:
7453+
if mcolors.is_color_like(color):
7454+
color = [color]*nx
74507455
colors = mcolors.to_rgba_array(color)
74517456
if len(colors) != nx:
74527457
raise ValueError(f"The 'color' keyword argument must have one "

lib/matplotlib/tests/test_axes.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2580,6 +2580,15 @@ def test_hist_zorder(histtype, zorder):
25802580
assert patch.get_zorder() == zorder
25812581

25822582

2583+
def test_hist_single_color_multiple_datasets():
2584+
data = [[0, 1, 2], [3, 4, 5]]
2585+
_, _, bar_containers = plt.hist(data, color='k')
2586+
for p in bar_containers[0].patches:
2587+
assert mcolors.same_color(p.get_facecolor(), 'k')
2588+
for p in bar_containers[1].patches:
2589+
assert mcolors.same_color(p.get_facecolor(), 'k')
2590+
2591+
25832592
def test_stairs_no_baseline_fill_warns():
25842593
fig, ax = plt.subplots()
25852594
with pytest.warns(UserWarning, match="baseline=None and fill=True"):

0 commit comments

Comments
 (0)