neves
neves

Reputation: 39213

Matplotlib: is it possible to do a stepwise stacked plot?

I'd like to make a stacked area chart but it would increase stepwise, like the stairs plot.

It is a cumulative chart, so a stepwise increase would make more sense.

How can it be done?

Upvotes: 2

Views: 912

Answers (1)

JohanC
JohanC

Reputation: 80329

plt.stackplot accepts extra kwargs which are sent to plt.fill_between. One of those is step='post', creating a horizontal line starting with the given value. (In contrast, step='pre' has the horizontal lines at the height of the ending positions.)

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(1, 6)
y1 = np.random.rand(5) + 1
y2 = np.random.rand(5) + 2
y3 = np.random.rand(5) + 3

plt.stackplot(x, y1, y2, y3, step='post', labels=['A', 'B', 'C'])
plt.xticks(x)
plt.legend()
plt.show()

plt.stackplot with step function

Upvotes: 2

Related Questions