Skip to main content
 首页 » 编程设计

python之Matplotlib 累积图

2025年05月04日15jillzhang

我应该使用什么 matplotlib 函数来创建这样的“上方”图?

我试图在 gallery 中找到它,但我没能做到。

请您参考如下方法:

你可以使用 matplotlib fill_between,例如,

#!/usr/bin/env python 
import matplotlib.pyplot as plt 
import numpy as np 
 
x = np.arange(0.0, 2, 0.1) 
y = 10*np.ones(x.shape[0]) 
c = ['r', 'b', 'g', 'y', 'k'] 
fig, ax = plt.subplots() 
for i in range(5): 
    r = np.random.randn(x.shape[0]) 
    yp = np.copy(y) 
    y = y + np.cumsum(np.abs(r)) 
    ax.fill_between(x, yp, y, color=c[i], alpha=0.5) 
 
plt.show() 

看起来,