Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8.1k
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
base:main
Are you sure you want to change the base?
Enh/Add hatch pattern support to Axes.grouped_bar#30726
Uh oh!
There was an error while loading.Please reload this page.
Conversation
3b86aaa todc94475CompareUh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
ilakkmanoharan commentedNov 5, 2025
ilakkmanoharan commentedNov 5, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Current behavior of colors and hatch:When colors=None: Matplotlib automatically cycles through the property color cycle (axes.prop_cycle), giving each dataset a distinct color by default. When colors is a single string (e.g. 'blue'): Since strings are iterable, itertools.cycle('blue') will incorrectly iterate over the characters 'b', 'l', 'u', 'e', producing unintended results. To apply one color to all datasets, use a single-element list, like ['blue']. When colors is a single-element list (e.g. ['blue']): The same color is applied to all datasets. When colors is a sequence (e.g. ['#1f77b4', '#ff7f0e', '#2ca02c']): The sequence is wrapped in itertools.cycle(colors). If there are more datasets than colors, the list repeats from the beginning, cycling through the same colors again. If there are fewer datasets than colors, only the needed colors are used; the extra colors are ignored. This behavior matches how color lists work in bar() and stackplot(). When colors is an empty list ([]): Treated as None, so the default property color cycle is used. When colors is a non-iterable (e.g. 123): A TypeError occurs since it cannot be cycled. For hatch: When hatch=None: Hatching is disabled entirely (itertools.cycle([None])), making hatching explicitly opt-in. When hatch is a single string (e.g. '//'): Raises a ValueError: "‘hatch’ must be a sequence of strings (e.g., ['//']) or None; a single string like '//' is not allowed." This prevents accidental iteration over individual characters ('/', '/'). When hatch is a single-element list (e.g. ['//']): The same hatch pattern is applied to all datasets. Because the list is wrapped with itertools.cycle(hatch), it repeats the single entry indefinitely — so every dataset receives '//'. Users can use this form to apply one uniform hatch pattern across all datasets. When hatch is a sequence of strings (e.g. ['//', 'xx', '..']): The sequence is wrapped in itertools.cycle(hatch). If there are more datasets than hatch patterns, the patterns repeat from the beginning, cycling through the same sequence again. If there are fewer datasets than hatch patterns, only the first few are used; the extras are ignored. This allows flexible per-dataset hatching without enforcing strict length matching. When hatch is an empty list ([]): Treated as None, disabling hatching. When hatch is non-iterable (e.g. 123): Raises a ValueError, since it cannot be converted into a sequence. When hatch contains non-string entries (e.g. [1, 2, 3]): Raises a TypeError — all entries must be strings representing valid hatch patterns. |
ilakkmanoharan commentedNov 5, 2025
When hatch is a single-element list (e.g. ['//']): The same hatch pattern is applied to all datasets. When colors is a single-element list (e.g. ['blue']): The same color is applied to all datasets. So we already have this behavior -->>> A single hatch pattern applied to all bars Also, we can also choose to provide a sequence of default hatch patterns if needed. |
ilakkmanoharan commentedNov 5, 2025
When colors=None: Matplotlib automatically cycles through the property color cycle (axes.prop_cycle), giving each dataset a distinct color by default. When colors is an empty list ([]): Treated as None, so the default property color cycle is used. So I wanted to mimic the same behavior for hatch too |
rcomer commentedNov 5, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
I'm sorry but I'm out. I am finding the many notifications and lengthy comments from@ilakkmanoharan quite wearing and I do not plan on engaging further with their contributions. |
ilakkmanoharan commentedNov 5, 2025
Should hatching in grouped_bar follow the property-cycle, as color does. |
story645 commentedNov 5, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
This question has been answered a few times already and the answer is no, hatching should not follow the default property cycle. Mostly because Matplotlib doesn't even have a default property cycle for hatch. Which if you would like this PR to be reviewed, please directly engage with the feedback you have been given. If you do not understand the feedback, please ask questions. Your current behavior suggests that you are either a bot or posting output to and from an AI tool without even reading it, both of which are reasons for us to close this PR. |
ilakkmanoharan commentedNov 6, 2025
Should hatching in grouped_bar follow the property-cycle, as color does. ---->> The reviewers have emphasized that the hatch should mimic the behavior of color. However, there’s a key difference here — while color has a default property-cycle behavior when set to None or an empty list, hatch currently does not. This is the first time I’m raising the question of whether hatch should also adopt a similar default (i.e., cycle through a predefined set of hatch patterns) when None or an empty list is provided. |
story645 commentedNov 6, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
No, the preference is for no hatch default set b/c the convention in visualization is generally no hatch. |
timhoffm commentedNov 6, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
To clarify hatch handling and similarity with color once and for all:
|
WeatherGod commentedNov 6, 2025 via email
An important additional detail is that the fact that there is a builtincolor cycling is the result of backwards compatibility/legacy behavior.When we added general property cycling, we made it so that there is anultimate failover of maintaining the old color cycling behavior. So, thereis "default" in the sense of whatever is in the rc params, and then thereis "default default" in the case of no rc param specified to serve as adefault. …On Thu, Nov 6, 2025 at 5:56 AM Tim Hoffmann ***@***.***> wrote: *timhoffm* left a comment (matplotlib/matplotlib#30726) <#30726 (comment)> *To clarify hatch handling and similarity with color once and for all:* - *Default*: no hatch - *Cycling of the default property cycle*: Do not add special code for this in grouped_bar(). grouped_bar() is essentially a sequence of bar calls. The color cycling is an emergent feature of the sequence of bar() calls. If there's anything missing for hatch, it would be respecting hatch entries in the cycler within bar(). That's a completely separte topic. — Reply to this email directly, view it on GitHub <#30726 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AACHF6C4C2HYENPB5SEAM4D33MSNHAVCNFSM6AAAAACLEPADH6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTIOJWGU2TGOJVHE> . You are receiving this because you are subscribed to this thread.Message ID: ***@***.***> |
timhoffm commentedNov 6, 2025
@WeatherGod thanks for the detail. I was not aware of that. |
ilakkmanoharan commentedNov 6, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
I agree, supporting hatching (hatch) through the property cycler (like colors are) — i.e., letting bar() automatically pull hatch values from the axes.prop_cycle — should be a separate topic -- that's why I created :#30728 The current behavior already matches how colors work when hatch is None. Changing it to not cycle would cause an error in the zip call since None isn’t iterable. Do you suggest that I remove cycling when hatch is None even if it requires more changes in the code?? |
ilakkmanoharan commentedNov 6, 2025
hatches = None → Not iterable; zip(heights, labels, colors, hatches) fails. |
ilakkmanoharan commentedNov 6, 2025
Hi everyone — I noticed test_backend_tk.py occasionally times out on Azure CI (e.g., Subprocess timed out in test_figuremanager_cleans_own_mainloop), while locally these tests are correctly skipped when tkinter isn’t available. |
Uh oh!
There was an error while loading.Please reload this page.
| # Empty sequence → treat as no hatch. | ||
| hatches=itertools.cycle([None]) |
There was a problem hiding this comment.
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".
ilakkmanoharanNov 7, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
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])There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 - compare
stackplot(..., hatch) - If in doubt, keep the API minimal. - It's much simpler to later extend the API than to narrow it.
There was a problem hiding this comment.
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.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
timhoffm commentedNov 6, 2025
The hatch cycling behavior is now as intended (with the |
timhoffm commentedNov 6, 2025
Sorry wrong button. Closing was unintended. |
Uh oh!
There was an error while loading.Please reload this page.
ilakkmanoharan commentedNov 7, 2025
story645 commentedNov 7, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Can you rebase? Which IDE are you using? I can also rebase for you and push to this PR. |
Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>
Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>
09622de to9fffd16Compareilakkmanoharan commentedNov 7, 2025
Rebased and removed PR cleanliness error. What are the next steps? |
ilakkmanoharan commentedNov 7, 2025
ilakkmanoharan commentedNov 7, 2025
#30729 (comment) --- I am seeing your comments here just now. Do you suggest that I handle this change too, in this pull request? If so, I will take care of it tomorrow. |
timhoffm commentedNov 8, 2025
Yes, please. |
ilakkmanoharan commentedNov 8, 2025
I’ve been thinking and decided to keep the common_kwargs refactor as a separate PR. This helps avoid scope creep in the current PR, which is focused on hatch support, and keeps the review simpler by separating behavioral changes from structural cleanup. It also improves visibility for future maintenance, since the shared-kwargs pattern could be applied more broadly later. I would like to open a new PR for this. Please advise regarding this. |
timhoffm commentedNov 8, 2025
This is generally a good strategy. However, the cleanup is tiny. It’s a three-line change, two of which are anyway touched by this PR. So there’s no real disadvantage in doing it right away. But I also won’t object if you do separate PRs. |
ilakkmanoharan commentedNov 8, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Thank you@timhoffm. I appreciate it. I would make that change in this PR itself. |
ilakkmanoharan commentedNov 8, 2025
Done I had to break the line into multiple lines due to Ruff line-length violation |
c7c5ade to9fffd16Compareilakkmanoharan commentedNov 8, 2025
@ilakkmanoharan I am removing this latest commit. I will create a new PR for the common_kwargs changes as it requires more changes. |
Uh oh!
There was an error while loading.Please reload this page.
Closes#30673
PR summary
This PR enhances the provisional Axes.grouped_bar() API by introducing per-dataset hatch pattern support
Supersedes#30683 — rebased cleanly to resolve CI pr_clean conflict.
Summary of Changes
Added a new hatch keyword argument to Axes.grouped_bar().
Updated the docstring to document hatch behavior and ValueError conditions.
Integrated hatch patterns into the plotting logic for both orientations:
Passed hatch=hatch_pattern to each call of .bar() and .barh().
Implemented validation and cycling behavior consistent with colors:
If hatch=None, no hatching is applied.
If a sequence is provided (e.g., ['//', 'xx', '..']), patterns are cycled across datasets.
Single string inputs (e.g., '//') or non-iterable values raise a ValueError.
Ensured that hatches are applied consistently across vertical and horizontal orientations.