Skip to content

Commit

Permalink
Added New Feature for different hatch color and edge color in bar plot
Browse files Browse the repository at this point in the history
  • Loading branch information
Vashesh08 committed Sep 3, 2023
1 parent cd7b3e9 commit 836382c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2262,7 +2262,7 @@ def _convert_dx(dx, x0, xconv, convert):

@_preprocess_data()
@_docstring.dedent_interpd
def bar(self, x, height, width=0.8, bottom=None, *, align="center",
def bar(self, x, height, width=0.8, bottom=None, *, align="center", edgecolor="black",
**kwargs):
r"""
Make a bar plot.
Expand Down Expand Up @@ -2320,6 +2320,8 @@ def bar(self, x, height, width=0.8, bottom=None, *, align="center",
edgecolor : color or list of color, optional
The colors of the bar edges.
hatchcolor : color of the hatch inside the bar
linewidth : float or array-like, optional
Width of the bar edge(s). If 0, don't draw edges.
Expand Down Expand Up @@ -2386,6 +2388,9 @@ def bar(self, x, height, width=0.8, bottom=None, *, align="center",
edgecolor = kwargs.pop('edgecolor', None)
linewidth = kwargs.pop('linewidth', None)
hatch = kwargs.pop('hatch', None)
hatchcolor = kwargs.get('hatchcolor', None)
if hatchcolor and not edgecolor:
edgecolor = "black"

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

if hatchcolor is None:
hatchcolor = itertools.repeat(None)
else:
hatchcolor = itertools.chain(
itertools.cycle(mcolors.to_rgba_array(hatchcolor)),
# Fallback if edgecolor == "none".
itertools.repeat('none'))

# We will now resolve the alignment and really have
# left, bottom, width, height vectors
_api.check_in_list(['center', 'edge'], align=align)
Expand Down
6 changes: 6 additions & 0 deletions lib/matplotlib/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def __init__(self, *,
fill=True,
capstyle=None,
joinstyle=None,
hatchcolor=None,
**kwargs):
"""
The following kwarg properties are supported
Expand All @@ -82,6 +83,7 @@ def __init__(self, *,
else:
self.set_edgecolor(edgecolor)
self.set_facecolor(facecolor)
self.set_hatchcolor(hatchcolor)

self._linewidth = 0
self._unscaled_dash_pattern = (0, None) # offset, dash
Expand Down Expand Up @@ -371,6 +373,10 @@ def set_color(self, c):
self.set_facecolor(c)
self.set_edgecolor(c)

def set_hatchcolor(self, c):
if c is not None:
self._hatch_color = colors.to_rgba(c)

def set_alpha(self, alpha):
# docstring inherited
super().set_alpha(alpha)
Expand Down

0 comments on commit 836382c

Please sign in to comment.