Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[ENH]: Multi hatching in ax.stackplot() #27146

Copy link
Copy link
Closed
@pawjast

Description

@pawjast
Issue body actions

Problem

ax.stackplot() is used for conveniently plotting stacked area plot.

The documentation says that any **kwargs is passed on to ax.fill_between.

So, it all works fine if I work with colours, but not so much with hatching pattern.

If I want to add hatching, this is what I get by default:

import matplotlib.pyplot as plt
import numpy as np

# Data
cols = 10
rows = 4

data = (
    np.reshape(np.arange(0, cols, 1), (1, -1)) ** 2
    + np.reshape(np.arange(0, rows), (-1, 1))
    + np.random.random((rows, cols))*5
)

# Plot
fig, ax = plt.subplots(
    figsize=(10,6),
    facecolor="white",
    layout="constrained"
)
fig.suptitle(
    "with hatching",
    fontsize=25,
    weight="bold"
)

x = range(data.shape[1])
ax.stackplot(
    x, data,
    hatch="x"
)

ax.set_xlim(0, 9)
ax.set_ylim(0, 350)

The outcome:
image

But I would be more interested in getting something like this:
image

Proposed solution

The calling signature would be the same as before with exception that the hatch parameter would accept e.g. a list with hatching styles you want to use.

Example:

ax.stackplot(
    x, data,
    hatch=["x", "\\", ".", "-"]
)

The example I've used above in the problem was create using the code below and in this case it varies the hatching by the density:

fig, ax = plt.subplots(
    figsize=(10,6),
    facecolor="white",
    layout="constrained"
)
fig.suptitle(
    "with individual hatching",
    fontsize=25,
    weight="bold"
)

x = range(data.shape[1])
stack_baseline = np.zeros((data.shape[1]))
for i, y in enumerate(data):
    ax.fill_between(
        x, stack_baseline, y+stack_baseline,
        facecolor=(1, 1, 1, 0),
        hatch="x"*(data.shape[0]-i)
    )
    stack_baseline += y

ax.set_xlim(0, 9)
ax.set_ylim(0, 350)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Good first issueOpen a pull request against these issues if there are no active ones!Open a pull request against these issues if there are no active ones!New feature

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.