Skip to content
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
5 changes: 5 additions & 0 deletions doc/release/next_whats_new/hist_color.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
``hist()`` supports a single color for multiple datasets
--------------------------------------------------------

It is now possible to pass a single *color* value to `~.Axes.hist()`. This
value is applied to all datasets.
5 changes: 5 additions & 0 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7326,6 +7326,9 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
Color or sequence of colors, one per dataset. Default (``None``)
uses the standard line color sequence.

.. versionadded:: 3.10
It is now possible to use a single color with multiple datasets.

label : str or list of str, optional
String, or sequence of strings to match multiple datasets. Bar
charts yield multiple patches per dataset, but only the first gets
Expand Down Expand Up @@ -7447,6 +7450,8 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
if color is None:
colors = [self._get_lines.get_next_color() for i in range(nx)]
else:
if mcolors.is_color_like(color):
color = [color]*nx
colors = mcolors.to_rgba_array(color)
if len(colors) != nx:
raise ValueError(f"The 'color' keyword argument must have one "
Expand Down
9 changes: 9 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2580,6 +2580,15 @@ def test_hist_zorder(histtype, zorder):
assert patch.get_zorder() == zorder


def test_hist_single_color_multiple_datasets():
data = [[0, 1, 2], [3, 4, 5]]
_, _, bar_containers = plt.hist(data, color='k')
for p in bar_containers[0].patches:
assert mcolors.same_color(p.get_facecolor(), 'k')
for p in bar_containers[1].patches:
assert mcolors.same_color(p.get_facecolor(), 'k')


def test_stairs_no_baseline_fill_warns():
fig, ax = plt.subplots()
with pytest.warns(UserWarning, match="baseline=None and fill=True"):
Expand Down
Loading