我想知道是否有办法用垂直渐变填充pyplot曲线,就像在这个快速模型中一样:
我在StackOverflow上发现了这个黑客,如果我能弄明白如何使彩色地图垂直,我不介意多边形:How to fill rainbow color under a curve in Python matplotlib
答案 0 :(得分:9)
可能有更好的方法,但这里有:
from matplotlib import pyplot as plt
x = range(10)
y = range(10)
z = [[z] * 10 for z in range(10)]
num_bars = 100 # more bars = smoother gradient
plt.contourf(x, y, z, num_bars)
background_color = 'w'
plt.fill_between(x, y, y2=max(y), color=background_color)
plt.show()
节目:
答案 1 :(得分:4)
在问题中更接近草图的替代解决方案。它是在Henry Barthes'上给出的。博客http://rechargeitlatter.blogspot.com/2014/06/filling-between-curves-with-color.html。 这对每个补丁都应用了一个imshow,我已经复制了代码,以防链接发生变化,
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.path import Path
from matplotlib.patches import PathPatch
xx=np.arange(0,10,0.01)
yy=xx*np.exp(-xx)
path = Path(np.array([xx,yy]).transpose())
patch = PathPatch(path, facecolor='none')
plt.gca().add_patch(patch)
im = plt.imshow(xx.reshape(yy.size,1),
cmap=plt.cm.Reds,
interpolation="bicubic",
origin='lower',
extent=[0,10,-0.0,0.40],
aspect="auto",
clip_path=patch,
clip_on=True)
plt.show()