Skip to content

Commit 836382c

Browse files
committed
Added New Feature for different hatch color and edge color in bar plot matplotlib#26074
1 parent cd7b3e9 commit 836382c

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎lib/matplotlib/axes/_axes.py‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2262,7 +2262,7 @@ def _convert_dx(dx, x0, xconv, convert):
22622262

22632263
@_preprocess_data()
22642264
@_docstring.dedent_interpd
2265-
def bar(self, x, height, width=0.8, bottom=None, *, align="center",
2265+
def bar(self, x, height, width=0.8, bottom=None, *, align="center", edgecolor="black",
22662266
**kwargs):
22672267
r"""
22682268
Make a bar plot.
@@ -2320,6 +2320,8 @@ def bar(self, x, height, width=0.8, bottom=None, *, align="center",
23202320
edgecolor : color or list of color, optional
23212321
The colors of the bar edges.
23222322
2323+
hatchcolor : color of the hatch inside the bar
2324+
23232325
linewidth : float or array-like, optional
23242326
Width of the bar edge(s). If 0, don't draw edges.
23252327
@@ -2386,6 +2388,9 @@ def bar(self, x, height, width=0.8, bottom=None, *, align="center",
23862388
edgecolor = kwargs.pop('edgecolor', None)
23872389
linewidth = kwargs.pop('linewidth', None)
23882390
hatch = kwargs.pop('hatch', None)
2391+
hatchcolor = kwargs.get('hatchcolor', None)
2392+
if hatchcolor and not edgecolor:
2393+
edgecolor = "black"
23892394

23902395
# Because xerr and yerr will be passed to errorbar, most dimension
23912396
# checking and processing will be left to the errorbar method.
@@ -2489,6 +2494,14 @@ def bar(self, x, height, width=0.8, bottom=None, *, align="center",
24892494
# Fallback if edgecolor == "none".
24902495
itertools.repeat('none'))
24912496

2497+
if hatchcolor is None:
2498+
hatchcolor = itertools.repeat(None)
2499+
else:
2500+
hatchcolor = itertools.chain(
2501+
itertools.cycle(mcolors.to_rgba_array(hatchcolor)),
2502+
# Fallback if edgecolor == "none".
2503+
itertools.repeat('none'))
2504+
24922505
# We will now resolve the alignment and really have
24932506
# left, bottom, width, height vectors
24942507
_api.check_in_list(['center', 'edge'], align=align)

‎lib/matplotlib/patches.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def __init__(self, *,
5656
fill=True,
5757
capstyle=None,
5858
joinstyle=None,
59+
hatchcolor=None,
5960
**kwargs):
6061
"""
6162
The following kwarg properties are supported
@@ -82,6 +83,7 @@ def __init__(self, *,
8283
else:
8384
self.set_edgecolor(edgecolor)
8485
self.set_facecolor(facecolor)
86+
self.set_hatchcolor(hatchcolor)
8587

8688
self._linewidth = 0
8789
self._unscaled_dash_pattern = (0, None) # offset, dash
@@ -371,6 +373,10 @@ def set_color(self, c):
371373
self.set_facecolor(c)
372374
self.set_edgecolor(c)
373375

376+
def set_hatchcolor(self, c):
377+
if c is not None:
378+
self._hatch_color = colors.to_rgba(c)
379+
374380
def set_alpha(self, alpha):
375381
# docstring inherited
376382
super().set_alpha(alpha)

0 commit comments

Comments
 (0)