Skip to content

Stackplot hatching #27169

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions lib/matplotlib/stackplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


def stackplot(axes, x, *args,
labels=(), colors=None, baseline='zero',
labels=(), colors=None, hatch=None, baseline='zero',
**kwargs):
"""
Draw a stacked area plot.
Expand Down Expand Up @@ -55,6 +55,12 @@ def stackplot(axes, x, *args,

If not specified, the colors from the Axes property cycle will be used.

hatch : list of hatching styles, optional
A sequence of styles to be cycled through for filling the stacked areas.
The sequence need not be exactly the same length as the number
of provided *y*, in which case the styles will repeat from the
beginning.

data : indexable object, optional
DATA_PARAMETER_PLACEHOLDER

Expand All @@ -76,6 +82,11 @@ def stackplot(axes, x, *args,
else:
colors = (axes._get_lines.get_next_color() for _ in y)

if not hatch or isinstance(hatch, str):
hatch = itertools.cycle([hatch])
else:
hatch = itertools.cycle(hatch)

# Assume data passed has not been 'stacked', so stack it here.
# We'll need a float buffer for the upcoming calculations.
stack = np.cumsum(y, axis=0, dtype=np.promote_types(y.dtype, np.float32))
Expand Down Expand Up @@ -113,7 +124,9 @@ def stackplot(axes, x, *args,

# Color between x = 0 and the first array.
coll = axes.fill_between(x, first_line, stack[0, :],
facecolor=next(colors), label=next(labels, None),
facecolor=next(colors),
hatch=next(hatch),
label=next(labels, None),
**kwargs)
coll.sticky_edges.y[:] = [0]
r = [coll]
Expand All @@ -122,6 +135,7 @@ def stackplot(axes, x, *args,
for i in range(len(y) - 1):
r.append(axes.fill_between(x, stack[i, :], stack[i + 1, :],
facecolor=next(colors),
hatch=next(hatch),
label=next(labels, None),
**kwargs))
return r