Skip to content

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

@pawjast

Description

@pawjast

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!New feature

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy