diff --git a/lib/matplotlib/stackplot.py b/lib/matplotlib/stackplot.py index 2629593683e5..8b16b2f2e4e3 100644 --- a/lib/matplotlib/stackplot.py +++ b/lib/matplotlib/stackplot.py @@ -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. @@ -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 @@ -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)) @@ -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] @@ -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