Skip to content

Commit a8c8705

Browse files
Allow None entries in hatch list for grouped_bar()
1 parent adf2717 commit a8c8705

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3347,8 +3347,8 @@ def grouped_bar(self, heights, *, positions=None, group_spacing=1.5, bar_spacing
33473347
if not hatch_list:
33483348
# Empty sequence → treat as no hatch.
33493349
hatches = itertools.cycle([None])
3350-
elif not all(isinstance(h, str) for h in hatch_list):
3351-
raise TypeError("All entries in 'hatch' must be strings")
3350+
elif not all(h is None or isinstance(h, str) for h in hatch_list):
3351+
raise TypeError("All entries in 'hatch' must be strings or None")
33523352
else:
33533353
# Sequence of hatch patterns: cycle through them as needed.
33543354
# Example: hatch=['//', 'xx', '..'] → patterns repeat across datasets.

lib/matplotlib/tests/test_axes.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2334,15 +2334,24 @@ def test_grouped_bar_hatch_none():
23342334

23352335

23362336
def test_grouped_bar_empty_string_disables_hatch():
2337-
"""An empty string in the hatch list should result in no hatch for that dataset."""
2337+
"""
2338+
Empty strings or None in the hatch list should result in no hatch
2339+
for the corresponding dataset, while valid strings should apply
2340+
the hatch pattern normally.
2341+
"""
23382342
fig, ax = plt.subplots()
23392343
x = np.arange(3)
2340-
heights = [np.array([1, 2, 3]), np.array([2, 1, 2])]
2341-
hatches = ["", "xx"]
2344+
heights = [np.array([1, 2, 3]), np.array([2, 1, 2]), np.array([3, 2, 1])]
2345+
hatches = ["", "xx", None]
23422346
containers = ax.grouped_bar(heights, positions=x, hatch=hatches)
2347+
# Collect the hatch pattern for each bar in each dataset
23432348
counts = [[rect.get_hatch() for rect in bc] for bc in containers.bar_containers]
2344-
assert all(h == '' or h is None for h in counts[0]) # first dataset: no hatch
2345-
assert all(h == 'xx' for h in counts[1]) # second dataset: hatched
2349+
# First dataset: empty string disables hatch
2350+
assert all(h in ("", None) for h in counts[0])
2351+
# Second dataset: hatch pattern applied
2352+
assert all(h == "xx" for h in counts[1])
2353+
# Third dataset: None disables hatch
2354+
assert all(h in ("", None) for h in counts[2])
23462355

23472356

23482357
def test_grouped_bar_dict_with_labels_forbidden():

0 commit comments

Comments
 (0)