Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Enh/Add hatch pattern support to Axes.grouped_bar#30726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
ilakkmanoharan wants to merge14 commits intomatplotlib:main
base:main
Choose a base branch
Loading
fromilakkmanoharan:enh/grouped-bar-hatch-broadcast-v2
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
14 commits
Select commitHold shift + click to select a range
cd9ddcd
Enhance grouped_bar: refine hatch handling, type hints, and tests
ilakkmanoharanNov 4, 2025
6e3a7c8
Sync pyplot boilerplate after grouped_bar signature change
ilakkmanoharanNov 4, 2025
dc94475
Sync pyplot boilerplate after grouped_bar signature change
ilakkmanoharanNov 4, 2025
b402730
Remove ValueError docstring note, drop TODOs
ilakkmanoharanNov 5, 2025
90f0a22
Trigger CI rerun: retry Windows backend timeouts
ilakkmanoharanNov 5, 2025
0fad966
Trigger CI rerun: retry WebAgg timeout
ilakkmanoharanNov 5, 2025
1eb63f9
Trigger CI rerun: retry backend timeout
ilakkmanoharanNov 6, 2025
960914c
Trigger CI rerun: retry TkAgg timeout
ilakkmanoharanNov 6, 2025
adf2717
Update lib/matplotlib/axes/_axes.py
ilakkmanoharanNov 7, 2025
a8c8705
Allow None entries in hatch list for grouped_bar()
ilakkmanoharanNov 7, 2025
d2f1943
CI: rerun tests
ilakkmanoharanNov 7, 2025
afb64cb
Docstring: clarify 'hatch' parameter type as sequence of :mpltype: or…
ilakkmanoharanNov 7, 2025
485ef67
Update lib/matplotlib/axes/_axes.py
ilakkmanoharanNov 7, 2025
9fffd16
Clean up API change entry for grouped_bar hatch behavior
ilakkmanoharanNov 7, 2025
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
53 changes: 49 additions & 4 deletionslib/matplotlib/axes/_axes.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3050,7 +3050,7 @@ def broken_barh(self, xranges, yrange, align="bottom", **kwargs):
@_docstring.interpd
def grouped_bar(self, heights, *, positions=None, group_spacing=1.5, bar_spacing=0,
tick_labels=None, labels=None, orientation="vertical", colors=None,
**kwargs):
hatch=None,**kwargs):
"""
Make a grouped bar plot.

Expand DownExpand Up@@ -3190,6 +3190,15 @@ def grouped_bar(self, heights, *, positions=None, group_spacing=1.5, bar_spacing

If not specified, the colors from the Axes property cycle will be used.

hatch : sequence of :mpltype:`hatch` or None, optional
Hatch pattern(s) to apply per dataset.

- If ``None`` (default), no hatching is applied.
- If a sequence of strings is provided (e.g., ``['//', 'xx', '..']``),
the patterns are cycled across datasets.
- If the sequence contains a single element (e.g., ``['//']``),
the same pattern is repeated for all datasets.

**kwargs : `.Rectangle` properties

%(Rectangle:kwdoc)s
Expand DownExpand Up@@ -3318,6 +3327,33 @@ def grouped_bar(self, heights, *, positions=None, group_spacing=1.5, bar_spacing
# TODO: do we want to be more restrictive and check lengths?
colors = itertools.cycle(colors)

if hatch is None:
# No hatch specified: disable hatching entirely by cycling [None].
hatches = itertools.cycle([None])

elif isinstance(hatch, str):
raise ValueError("'hatch' must be a sequence of strings "
"(e.g., ['//']) or None; "
"a single string like '//' is not allowed."
)

else:
try:
hatch_list = list(hatch)
except TypeError:
raise ValueError("'hatch' must be a sequence of strings"
"(e.g., ['//']) or None") from None

if not hatch_list:
# Empty sequence → treat as no hatch.
hatches = itertools.cycle([None])
Comment on lines +3348 to +3349
Copy link
Member

Choose a reason for hiding this comment

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

Let's raise instead. Semantically,hatch=[] does not make sense. Users should useNone for "no hatch".

Copy link
Author

@ilakkmanoharanilakkmanoharanNov 7, 2025
edited
Loading

Choose a reason for hiding this comment

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

That makes sense semantically — None is the canonical way to disable hatching.
However, in practice, there are scenarios where hatch can be constructed programmatically (e.g., through loops, Giles, or data pipelines). In such cases, it may naturally or unintentionally evaluate to an empty list — for example, via something like config.get("hatch", []). Raising an error in these situations could make otherwise valid automation brittle.
Would it be acceptable to issue a warning and treat an empty list as None instead, to preserve robustness? This would align with Matplotlib’s general philosophy of being tolerant and user-friendly toward benign input while still informing users of the preferred approach.

if not hatch_list:    _api.warn_external(        "'hatch' was an empty sequence; treating as no hatch. "        "Use None explicitly to suppress this warning."    )    hatches = itertools.cycle([None])

Copy link
Member

Choose a reason for hiding this comment

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

I would rather argument, that it's likely an error in the programmatic code if it returns an empty list. I don't see a practical case in which one would naturally end up with an empty list if one has a finite set of groups and want to generate hatches.config.get("hatch", []) seems contrieved. Why would one want to write this instead ofconfig.get("hatch") which will work correctly.

Choose a reason for hiding this comment

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

However, in practice, empty sequences can occur naturally in data-driven workflows. For instance, configuration files often deserialize [] for optional arrays (e.g., JSON/YAML style templates), and plotting utilities may use defensive defaults like config.get("hatch", []) to avoid KeyError. Similarly, dynamically generating hatches from metadata — e.g. [meta.get("hatch") for meta in datasets] — can yield an empty list when all datasets omit that field.
In such cases, treating [] as “no hatch” (with a warning) keeps things robust and user-friendly for automated or programmatic pipelines, while still guiding users to prefer None explicitly.

Choose a reason for hiding this comment

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

@timhoffm Waiting for your response. What did you decide regarding this?

Choose a reason for hiding this comment

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

Even if these use cases may be rare or not widely observed at the moment, such scenarios could easily emerge in evolving data-driven or cloud-integrated visualization systems in the immediate future — often unexpectedly.

Copy link
Member

Choose a reason for hiding this comment

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

Dynamic data aggregation / filtering workflows:

Pipelines may dynamically build visual encodings (colors, hatches, etc.) from dataset metadata:

hatches = [meta.get("hatch") for meta in datasets if meta.get("include", True)]

Such code is outright dangerous. Depending on the distribution of "include" in your datasets the list can have an arbitrary length between 0 andlen(datasets), and all the in-between values result in undesired behavior. When infering from datasets you definitively want to have exactly one property per dataset:

hatches = [meta.get("hatch") if meta.get("include", True) else None           for meta in datasets]

All other cases are just hand-waving "but someone might end up with an empty list that they want to pass", which I don't buy. (btw. are these cases AI-genereated?) Until someone comes up with a concrete example, I have the firm believe that empty lists are rather an error in the generating code and not intended input.

Note that we also raise forplt.stackplot([1, 2, 3], [[1, 2 ,3]]*2, hatch=[]) - though admittedly in a less helpful way.

Choose a reason for hiding this comment

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

Thanks,@timhoffm — I appreciate your detailed perspective. I’d like to clarify why I still believe that gracefully handling an empty sequence (with a warning) can be beneficial in the long run, both pragmatically and philosophically.

1. Defensive and user-friendly design

Matplotlib’s strength has always been its tolerance toward benign irregularities. Users rely on it as a stable visualization backend even when inputs are indirectly produced — from templates, configs, or pipelines — not always hand-written by developers.

Treating [] as equivalent to None (with a warning) preserves this spirit of robustness and continuity. It avoids brittle failures in higher-level tools that integrate Matplotlib as a rendering engine, especially when such inputs originate from sources beyond direct user control.

2. Real-world automation and serialization

Even though empty lists may seem unlikely in manual usage, they appear quite often in automated systems:
YAML/JSON serialization — missing or optional fields frequently deserialize to empty arrays rather than None.
Dynamic workflows — templated plotting pipelines or report generators (e.g., Airflow, Prefect, or CI visualizations) often construct keyword dictionaries dynamically. When an aesthetic parameter is unused, it may remain as an empty list placeholder.

Interoperability layers — frameworks like seaborn, pandas, or holoviews may forward arguments programmatically, sometimes defaulting to empty sequences when certain aesthetics are unused.
These cases are not errors per se — they are side effects of flexible, data-driven design where “no hatch” may naturally translate to an empty sequence.

3. Design philosophy — protecting the fragile parts

From a design standpoint, the absence of current breakage does not mean the absence of potential breakage.
If a construct can occur naturally in evolving ecosystems, it’s prudent to make the library resilient to it — especially when doing so is low-risk and consistent with existing semantics (None already means “no hatch”).
Designing defensively means protecting things that could break, even without hard proof that they already do. In software ecosystems, “possible” often becomes “inevitable” over time as integration layers grow.

4. Minimal cost, maximal stability

Issuing a warning — rather than raising — strikes a balanced compromise:
It informs users of the preferred canonical input (None).
It preserves existing behavior for programmatic systems that may unintentionally produce [].
It avoids introducing regressions in automated pipelines where errors may not be easy to trace back to Matplotlib.

So, while I agree that hatch=[] might not be common in hand-written code, its graceful handling can future-proof Matplotlib against subtle integration issues in complex or automated visualization systems — where inputs are often constructed indirectly rather than typed explicitly.

In essence, this isn’t about accommodating an artificial case — it’s about maintaining the robustness and user-centered flexibility that have always defined Matplotlib’s design philosophy.

I’d like to open a separate issue to discuss this more broadly, since the handling of empty sequences (like hatch=[]) may have implications beyond this specific function.

This could be an opportunity to evaluate whether Matplotlib should adopt a consistent, system-wide approach toward benign empty inputs — for example, treating them as “no-op” equivalents with a warning, rather than raising outright.

Such a discussion could help clarify the library’s general stance on defensive input handling versus strict validation, especially in the context of data-driven or programmatic pipelines that interface with Matplotlib.

Personally, as a user, it feels more natural and intuitive to assign hatch = [], since it allows me to dynamically add or remove patterns later

Copy link
Member

Choose a reason for hiding this comment

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

I recognize you are trying to make your point. However, you are not responding to my points in#30726 (comment).In particular, please stop posting AI-generated content. A generic AI argument falls short of considering the nuances and context of API design here. I am not spending time on rebutting AI gibberish. (I'm sorry if my AI assumption is wrong, but the text feels very AI-generated and scanners say with 99% likelihood it is.)

These principles found my decision:

  • In the face of ambiguity, refuse the temptation to guess.
  • Errors should never pass silently.
  • Consistency - comparestackplot(..., hatch)
  • If in doubt, keep the API minimal. - It's much simpler to later extend the API than to narrow it.

Choose a reason for hiding this comment

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

I just wanted to point out a small consistency angle that might be worth considering. If None is accepted to mean “no hatch,” then treating an empty list [] the same way actually keeps the API simple rather than expanding it — both essentially express “no pattern.”

For example:

config = {"color": ["red", "blue"], "hatch": []}ax.bar(x, y, **config)

In a lot of configuration-driven setups (like JSON or YAML), optional arrays often deserialize as empty lists by default instead of None. Handling [] like None would make the behavior more predictable and tolerant in those cases, without changing the meaning or adding any new semantics.

This isn’t about guessing what the user wants — it’s more about keeping the two representations of “no hatch” consistent in data-driven workflows, where either form might appear naturally.

For instance, in dynamic plotting code, users might deliberately pass [] when they’re building visual attributes incrementally or conditionally:

hatches = []if use_patterns:    hatches.append('//')ax.bar(x, y, hatch=hatches)

In this kind of logic, hatches starts as an empty list by design — it’s not an error or a missing value, just a placeholder that may or may not be populated later depending on conditions. Passing [] deliberately keeps the code clean and avoids special-case checks like:

ax.bar(x, y, hatch=None if not hatches else hatches)

So, supporting [] as equivalent to None helps in situations where users or frameworks construct arguments dynamically, keeping code simpler and more consistent without altering semantics.

elif not all(h is None or isinstance(h, str) for h in hatch_list):
raise TypeError("All entries in 'hatch' must be strings or None")
else:
# Sequence of hatch patterns: cycle through them as needed.
# Example: hatch=['//', 'xx', '..'] → patterns repeat across datasets.
hatches = itertools.cycle(hatch)

bar_width = (group_distance /
(num_datasets + (num_datasets - 1) * bar_spacing + group_spacing))
bar_spacing_abs = bar_spacing * bar_width
Expand All@@ -3331,15 +3367,24 @@ def grouped_bar(self, heights, *, positions=None, group_spacing=1.5, bar_spacing
# place the bars, but only use numerical positions, categorical tick labels
# are handled separately below
bar_containers = []
for i, (hs, label, color) in enumerate(zip(heights, labels, colors)):

# Both colors and hatches are cycled indefinitely using itertools.cycle.
# heights and labels, however, are finite (length = num_datasets).
# Because zip() stops at the shortest iterable, this loop executes exactly
# num_datasets times even though colors and hatches are infinite.
# This ensures one (color, hatch) pair per dataset
# without explicit length checks.
for i, (hs, label, color, hatch_pattern) in enumerate(
zip(heights, labels, colors, hatches)
):
lefts = (group_centers - 0.5 * group_distance + margin_abs
+ i * (bar_width + bar_spacing_abs))
if orientation == "vertical":
bc = self.bar(lefts, hs, width=bar_width, align="edge",
label=label, color=color, **kwargs)
label=label, color=color,hatch=hatch_pattern,**kwargs)
else:
bc = self.barh(lefts, hs, height=bar_width, align="edge",
label=label, color=color, **kwargs)
label=label, color=color,hatch=hatch_pattern,**kwargs)
bar_containers.append(bc)

if tick_labels is not None:
Expand Down
1 change: 1 addition & 0 deletionslib/matplotlib/axes/_axes.pyi
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -287,6 +287,7 @@ class Axes(_AxesBase):
bar_spacing: float | None = ...,
orientation: Literal["vertical", "horizontal"] = ...,
colors: Iterable[ColorType] | None = ...,
hatch: Iterable[str] | None = ...,
**kwargs
) -> list[BarContainer]: ...
def stem(
Expand Down
2 changes: 2 additions & 0 deletionslib/matplotlib/pyplot.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3536,6 +3536,7 @@ def grouped_bar(
labels: Sequence[str] | None = None,
orientation: Literal["vertical", "horizontal"] = "vertical",
colors: Iterable[ColorType] | None = None,
hatch: Iterable[str] | None = None,
**kwargs,
) -> list[BarContainer]:
return gca().grouped_bar(
Expand All@@ -3547,6 +3548,7 @@ def grouped_bar(
labels=labels,
orientation=orientation,
colors=colors,
hatch=hatch,
**kwargs,
)

Expand Down
104 changes: 104 additions & 0 deletionslib/matplotlib/tests/test_axes.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2267,6 +2267,110 @@ def test_grouped_bar_return_value():
assert bc not in ax.containers


def test_grouped_bar_single_hatch_str_raises():
"""Passing a single string for hatch should raise a ValueError."""
fig, ax = plt.subplots()
x = np.arange(3)
heights = [np.array([1, 2, 3]), np.array([2, 1, 2])]
with pytest.raises(ValueError, match="must be a sequence of strings"):
ax.grouped_bar(heights, positions=x, hatch='//')


def test_grouped_bar_hatch_non_iterable_raises():
"""Non-iterable hatch values should raise a ValueError."""
fig, ax = plt.subplots()
heights = [np.array([1, 2]), np.array([2, 3])]
with pytest.raises(ValueError, match="must be a sequence of strings"):
ax.grouped_bar(heights, hatch=123) # invalid non-iterable


def test_grouped_bar_hatch_sequence():
"""Each dataset should receive its own hatch pattern when a sequence is passed."""
fig, ax = plt.subplots()
x = np.arange(2)
heights = [np.array([1, 2]), np.array([2, 3]), np.array([3, 4])]
hatches = ['//', 'xx', '..']
containers = ax.grouped_bar(heights, positions=x, hatch=hatches)

# Verify each dataset gets the corresponding hatch
for hatch, c in zip(hatches, containers.bar_containers):
for rect in c:
assert rect.get_hatch() == hatch


def test_grouped_bar_hatch_cycles_when_shorter_than_datasets():
"""When the hatch list is shorter than the number of datasets,
patterns should cycle.
"""

fig, ax = plt.subplots()
x = np.arange(2)
heights = [
np.array([1, 2]),
np.array([2, 3]),
np.array([3, 4]),
]
hatches = ['//', 'xx'] # shorter than number of datasets → should cycle
containers = ax.grouped_bar(heights, positions=x, hatch=hatches)

expected_hatches = ['//', 'xx', '//'] # cycle repeats
for gi, c in enumerate(containers.bar_containers):
for rect in c:
assert rect.get_hatch() == expected_hatches[gi]


def test_grouped_bar_hatch_none():
"""Passing hatch=None should result in bars with no hatch."""
fig, ax = plt.subplots()
x = np.arange(2)
heights = [np.array([1, 2]), np.array([2, 3])]
containers = ax.grouped_bar(heights, positions=x, hatch=None)

# All bars should have no hatch applied
for c in containers.bar_containers:
for rect in c:
assert rect.get_hatch() in (None, ''), \
f"Expected no hatch, got {rect.get_hatch()!r}"


def test_grouped_bar_empty_string_disables_hatch():
"""
Empty strings or None in the hatch list should result in no hatch
for the corresponding dataset, while valid strings should apply
the hatch pattern normally.
"""
fig, ax = plt.subplots()
x = np.arange(3)
heights = [np.array([1, 2, 3]), np.array([2, 1, 2]), np.array([3, 2, 1])]
hatches = ["", "xx", None]
containers = ax.grouped_bar(heights, positions=x, hatch=hatches)
# Collect the hatch pattern for each bar in each dataset
counts = [[rect.get_hatch() for rect in bc] for bc in containers.bar_containers]
# First dataset: empty string disables hatch
assert all(h in ("", None) for h in counts[0])
# Second dataset: hatch pattern applied
assert all(h == "xx" for h in counts[1])
# Third dataset: None disables hatch
assert all(h in ("", None) for h in counts[2])


def test_grouped_bar_dict_with_labels_forbidden():
"""Passing labels along with dict input should raise an error."""
fig, ax = plt.subplots()
data = {"a": [1, 2], "b": [2, 1]}
with pytest.raises(ValueError, match="cannot be used if 'heights' is a mapping"):
ax.grouped_bar(data, labels=["x", "y"])


def test_grouped_bar_positions_not_equidistant():
"""Passing non-equidistant positions should raise an error."""
fig, ax = plt.subplots()
x = np.array([0, 1, 3])
heights = [np.array([1, 2, 3]), np.array([2, 1, 2])]
with pytest.raises(ValueError, match="must be equidistant"):
ax.grouped_bar(heights, positions=x)


def test_boxplot_dates_pandas(pd):
# smoke test for boxplot and dates in pandas
data = np.random.rand(5, 2)
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp