3

I want to create a stackplot using python matplotlib library, which I did as on the below pseudocode

plt.stackplot(x,
          [first_value, second_value, third_value], 
          colors=['color1','color2','color3'])

However I want to add to each part (i.e., first_value, second_value, third_value) different hatches, but the hatch command works on the whole plot. How can I pass to the stackplot a list of different hatches?

1 Answer 1

10

The stackplot does not have an argument to set the hatching individually on the different polygons. So the idea would be to loop over the different stacks and set a respective hatching.

stackplot returns a list of all the stacks we can use for that purpose.

import numpy as np; np.random.seed(1)
import matplotlib.pyplot as plt

fnx = lambda x: np.random.randint(5, 50, 10)
y = np.row_stack((fnx(0), fnx(0), fnx(0)))
x = np.arange(10)

fig, ax = plt.subplots()
stacks = ax.stackplot(x, y)

hatches=["\\", "//","+"]
for stack, hatch in zip(stacks, hatches):
    stack.set_hatch(hatch)

plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Is it possible to change the hatch colours?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.