From 7b7a06b404c22b7e62bfd816089e8e8fb6f7f303 Mon Sep 17 00:00:00 2001 From: nbarlowATI Date: Fri, 20 Oct 2023 16:45:17 +0100 Subject: [PATCH 1/3] First attempt for individual hatching styles for stackplot --- lib/matplotlib/stackplot.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/stackplot.py b/lib/matplotlib/stackplot.py index 2629593683e5..a88ee5b61d4b 100644 --- a/lib/matplotlib/stackplot.py +++ b/lib/matplotlib/stackplot.py @@ -6,6 +6,7 @@ (https://stackoverflow.com/users/66549/doug) """ +from collections.abc import Iterable import itertools import numpy as np @@ -16,7 +17,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 +56,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 +83,14 @@ def stackplot(axes, x, *args, else: colors = (axes._get_lines.get_next_color() for _ in y) + if hatch: + if isinstance(hatch, Iterable): + hatch = itertools.cycle(hatch) + else: + hatch = (hatch for _ in y) + else: + hatch = (" " for _ in y) + # 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 +128,7 @@ 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 +137,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 From 11591b6b17aa2a751d058b089c95f6ceaddf87f7 Mon Sep 17 00:00:00 2001 From: nbarlowATI Date: Fri, 20 Oct 2023 17:07:43 +0100 Subject: [PATCH 2/3] break up overlong line --- lib/matplotlib/stackplot.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/stackplot.py b/lib/matplotlib/stackplot.py index a88ee5b61d4b..f92a0973cb18 100644 --- a/lib/matplotlib/stackplot.py +++ b/lib/matplotlib/stackplot.py @@ -128,7 +128,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), hatch=next(hatch), label=next(labels, None), + facecolor=next(colors), + hatch=next(hatch), + label=next(labels, None), **kwargs) coll.sticky_edges.y[:] = [0] r = [coll] From 8773619d6798d6dae1ac4d76017e52d86d90a828 Mon Sep 17 00:00:00 2001 From: nbarlowATI Date: Sun, 22 Oct 2023 12:17:55 +0100 Subject: [PATCH 3/3] hatching in stackplot with strings or None for hatch argument --- lib/matplotlib/stackplot.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/matplotlib/stackplot.py b/lib/matplotlib/stackplot.py index f92a0973cb18..8b16b2f2e4e3 100644 --- a/lib/matplotlib/stackplot.py +++ b/lib/matplotlib/stackplot.py @@ -6,7 +6,6 @@ (https://stackoverflow.com/users/66549/doug) """ -from collections.abc import Iterable import itertools import numpy as np @@ -83,13 +82,10 @@ def stackplot(axes, x, *args, else: colors = (axes._get_lines.get_next_color() for _ in y) - if hatch: - if isinstance(hatch, Iterable): - hatch = itertools.cycle(hatch) - else: - hatch = (hatch for _ in y) + if not hatch or isinstance(hatch, str): + hatch = itertools.cycle([hatch]) else: - hatch = (" " for _ in y) + 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.