8000 Allow list of hatches to {bar, barh} · matplotlib/matplotlib@5af82a7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5af82a7

Browse files
committed
Allow list of hatches to {bar, barh}
1 parent 45d27c9 commit 5af82a7

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2366,6 +2366,7 @@ def bar(self, x, height, width=0.8, bottom=None, *, align="center",
23662366
color = self._get_patches_for_fill.get_next_color()
23672367
edgecolor = kwargs.pop('edgecolor', None)
23682368
linewidth = kwargs.pop('linewidth', None)
2369+
hatch = kwargs.pop('hatch', None)
23692370

23702371
# Because xerr and yerr will be passed to errorbar, most dimension
23712372
# checking and processing will be left to the errorbar method.
@@ -2427,9 +2428,9 @@ def bar(self, x, height, width=0.8, bottom=None, *, align="center",
24272428
if yerr is not None:
24282429
yerr = self._convert_dx(yerr, y0, y, self.convert_yunits)
24292430

2430-
x, height, width, y, linewidth = np.broadcast_arrays(
2431+
x, height, width, y, linewidth, hatch = np.broadcast_arrays(
24312432
# Make args iterable too.
2432-
np.atleast_1d(x), height, width, y, linewidth)
2433+
np.atleast_1d(x), height, width, y, linewidth, hatch)
24332434

24342435
# Now that units have been converted, set the tick locations.
24352436
if orientation == 'vertical':
@@ -2440,6 +2441,7 @@ def bar(self, x, height, width=0.8, bottom=None, *, align="center",
24402441
tick_label_position = y
24412442

24422443
linewidth = itertools.cycle(np.atleast_1d(linewidth))
2444+
hatch = itertools.cycle(np.atleast_1d(hatch))
24432445
color = itertools.chain(itertools.cycle(mcolors.to_rgba_array(color)),
24442446
# Fallback if color == "none".
24452447
itertools.repeat('none'))
@@ -2476,14 +2478,16 @@ def bar(self, x, height, width=0.8, bottom=None, *, align="center",
24762478
bottom = y
24772479

24782480
patches = []
2479-
args = zip(left, bottom, width, height, color, edgecolor, linewidth)
2480-
for l, b, w, h, c, e, lw in args:
2481+
args = zip(left, bottom, width, height, color, edgecolor, linewidth,
2482+
hatch)
2483+
for l, b, w, h, c, e, lw, htch in args:
24812484
r = mpatches.Rectangle(
24822485
xy=(l, b), width=w, height=h,
24832486
facecolor=c,
24842487
edgecolor=e,
24852488
linewidth=lw,
24862489
label='_nolegend_',
2490+
hatch=htch,
24872491
)
24882492
r.update(kwargs)
24892493
r.get_path()._interpolation_steps = 100

lib/matplotlib/tests/test_axes.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,6 +1551,20 @@ def test_bar_pandas_indexed(pd):
15511551
ax.bar(df.x, 1., width=df.width)
15521552

15531553

1554+
@check_figures_equal()
1555+
def test_bar_hatches(fig_test, fig_ref):
1556+
ax_test = fig_test.subplots()
1557+
ax_ref = fig_ref.subplots()
1558+
1559+
x = [1, 2]
1560+
y = [2, 3]
1561+
hatches = ['x', 'o']
1562+
for i in range(2):
1563+
ax_ref.bar(x[i], y[i], color='blue', hatch=hatches[i])
1564+
1565+
ax_test.bar(x, y, hatch=hatches)
1566+
1567+
15541568
def test_pandas_minimal_plot(pd):
15551569
# smoke test that series and index objcets do not warn
15561570
x = pd.Series([1, 2], dtype="float64")

0 commit comments

Comments
 (0)
0