在 Python matplotlib 中,如何获得折线图或阶梯图中的线以显示基于 y 值的梯度?
示例图(在 Tableau 中制作):
带有根据 x 值改变梯度的线的阶梯图代码,改编自此答案:
fig, ax = plt.subplots(figsize=(10, 4))
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
y = [2, 3, 9, 10, 2, 9, 0, 1, 9, 1, -8]
T = np.linspace(0,1,np.size(x))**2
s = 1
for i in range(0, len(x)-s, s):
ax.step(x[i:i+s+1], y[i:i+s+1], marker='.', color=(0.0,0.5,T[i]))
ax.tick_params(axis='both', colors='lightgray', labelsize=8)
慕沐林林
相关分类