Skip to content
Closed
Show file tree
Hide file tree
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
42 changes: 42 additions & 0 deletions doc/users/next_whats_new/stackplot_artifacts.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Suppress 0 thickness layers in stackplots
-----------------------------------------

When making `.Axes.stackplot` where one of the layers is 0 it may or may not be
desirable for that layer to be visible. Using the new *where* keyword argument
along with the *interpolate* keyword argument these lines can be suppressed.


.. plot::
:include-source: true

import matplotlib.pyplot as plt
import numpy as np

fig, (axl, axr) = plt.subplots(1, 2)

x = np.arange(10)

colors = ["grey", "blue", "red", "blue"]

y = np.stack(
[
np.linspace(0, 1, 10),
np.linspace(0, 1, 10),
[0, 0, 0.25, 0, 0, 0, 0.25, 0, 0, 0],
np.linspace(0, 1, 10),
]
)

axl.set_title("No line")
axl.stackplot(
x, y,
colors=colors, edgecolor="face",
where=(y != 0), interpolate=True,
)


axr.set_title("With line")
axr.stackplot(
x, y,
colors=colors, edgecolor="face",
)
7 changes: 4 additions & 3 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2872,11 +2872,12 @@ def spy(
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
@_copy_docstring_and_deprecators(Axes.stackplot)
def stackplot(
x, *args, labels=(), colors=None, baseline='zero', data=None,
**kwargs):
x, *args, labels=(), colors=None, baseline='zero',
where=None, data=None, **kwargs):
return gca().stackplot(
x, *args, labels=labels, colors=colors, baseline=baseline,
**({"data": data} if data is not None else {}), **kwargs)
where=where, **({"data": data} if data is not None else {}),
**kwargs)


# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
Expand Down
22 changes: 21 additions & 1 deletion lib/matplotlib/stackplot.py
100644 → 100755
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, baseline='zero', where=None,
**kwargs):
"""
Draw a stacked area plot.
Expand Down Expand Up @@ -58,6 +58,15 @@ def stackplot(axes, x, *args,
data : indexable object, optional
DATA_PARAMETER_PLACEHOLDER

where: bool or array of bool, optional

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm struggling with the where parameter. While it is reasonable solve the issue internally by passing where to fill_between, is there any reasonable value for where other than y!=0?

  • The generality of fill_between (where=) is not needed. It does not make sense to not plot regions in a stack plot where there are finite values. That would leave gaps.
  • I assume where needs the shape of y; i.e. must be 2D. Not having a resolution in x does not make sense. Using the same where array for all datasets should be the very exception; usually you need.

It seems like a simpler high-level API such as a boolean parameter clip_zero_regions should be enough, and that would internally use where=(y!=0).

Passed to `.Axes.fill_between` and defines where to exclude horizontal
regions from being filled. The filled regions are defined by the
coordinates ``x[where]``. Can be either a single bool, an array of
shape (N,) or an array of shape (M, N). Should be used together with
kwarg ``interpolate=True``.

.. versionadded:: 3.7

**kwargs
All other keyword arguments are passed to `.Axes.fill_between`.

Expand All @@ -66,10 +75,19 @@ def stackplot(axes, x, *args,
list of `.PolyCollection`
A list of `.PolyCollection` instances, one for each element in the
stacked area plot.

"""

y = np.row_stack(args)

if where is not None:
if kwargs.get('interpolate', False) is False:
_api.warn_external('Argument where should be used together with '
'kwarg interpolate=True')
else:
where = True
where = np.broadcast_to(where, np.shape(y))

labels = iter(labels)
if colors is not None:
colors = itertools.cycle(colors)
Expand Down Expand Up @@ -114,6 +132,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),
where=where[0, :],
**kwargs)
coll.sticky_edges.y[:] = [0]
r = [coll]
Expand All @@ -123,5 +142,6 @@ def stackplot(axes, x, *args,
r.append(axes.fill_between(x, stack[i, :], stack[i + 1, :],
facecolor=next(colors),
label=next(labels, None),
where=where[i+1, :],
**kwargs))
return r
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading