diff --git a/doc/release/next_whats_new/hist_color.rst b/doc/release/next_whats_new/hist_color.rst new file mode 100644 index 000000000000..484369655bef --- /dev/null +++ b/doc/release/next_whats_new/hist_color.rst @@ -0,0 +1,5 @@ +``hist()`` supports a single color for multiple datasets +-------------------------------------------------------- + +It is now possible pass a single *color* value to `~.Axes.hist()`. This value +is applied to all datasets. diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 470e096eb033..85c83d847782 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -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 @@ -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 " diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 6e839ef2f189..6493b144cbf9 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -2580,6 +2580,13 @@ def test_hist_zorder(histtype, zorder): assert patch.get_zorder() == zorder +def test_hist_single_color_multiple_datasets(): + # Test a single color for multiple datasets + # https://github.com/matplotlib/matplotlib/issues/30857 + data = [[0, 1, 2], [3, 4, 5]] + plt.hist(data, color='k') + + def test_stairs_no_baseline_fill_warns(): fig, ax = plt.subplots() with pytest.warns(UserWarning, match="baseline=None and fill=True"):