🌐 AI搜索 & 代理 主页
Skip to content
Open
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 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
7 changes: 7 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +2584 to +2585
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Test a single color for multiple datasets
# https://github.com/matplotlib/matplotlib/issues/30857

The content of the first line is already expressed through the function name.
We typically do not reference github issues. For future readers, it does not matter whether the test was added as part of the original implementation or through a later issue. If somebody really want to know this they can reconstruct the PR from the commit and the issue from the PR.

data = [[0, 1, 2], [3, 4, 5]]
plt.hist(data, color='k')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only tests that the call does not blow up. It does not verify whether the right thing happens. Please refine:

Suggested change
plt.hist(data, color='k')
_, _, bar_containers = plt.hist(data, color='k')
for p in bar_containers[0].patches:
assert mcolor.same_color(p.get_facecolor(), 'k')
for p in bar_containers[1].patches:
assert mcolor.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