在图表下设置颜色图 [英] Set a colormap under a graph
问题描述
我知道这已经被很好地证明了,但是我正在努力在我的代码中实现它.
I know this is well documented, but I'm struggling to implement this in my code.
我想用一个颜色贴图在我的图形下面的区域着色.是否有可能在30多个点上都具有一种颜色,即红色,直到该点为止都有渐变?
I would like to shade the area under my graph with a colormap. Is it possible to have a colour, i.e. red from any points over 30, and a gradient up until that point?
我正在使用fill_between方法,但是如果有更好的方法可以更改它,我很乐意.
I am using the method fill_between, but I'm happy to change this if there is a better way to do it.
def plot(sd_values):
plt.figure()
sd_values=np.array(sd_values)
x=np.arange(len(sd_values))
plt.plot(x,sd_values, linewidth=1)
plt.fill_between(x,sd_values, cmap=plt.cm.jet)
plt.show()
这是目前的结果.我已经尝试过 axvspan
,但这没有 cmap
作为选项.为什么下图没有显示颜色图?
This is the result at the moment. I have tried axvspan
, but this doesnt have cmap
as an option. Why does the below graph not show a colormap?
推荐答案
我不确定 cmap
参数是否应作为 fill_between
绘图命令的一部分.在您的情况下,可能要使用 fill()
命令btw.
I'm not sure if the cmap
argument should be part of the fill_between
plotting command. In your case probably want to use the fill()
command btw.
这些填充命令创建多边形或多边形集合.多边形集合可以使用 cmap
,但是使用 fill
时,无法提供应为其着色的数据.
These fill commands create polygons or polygon collections. A polygon collection can take a cmap
but with fill
there is no way of providing the data on which it should be colored.
(据我所知),当然不可能的是根据需要用渐变填充单个多边形.
What's (for as far as i know) certainly not possible is to fill a single polygon with a gradient as you wish.
下一个最好的办法是伪造它.您可以绘制阴影图像并根据创建的多边形对其进行裁剪.
The next best thing is to fake it. You can plot a shaded image and clip it based on the created polygon.
# create some sample data
x = np.linspace(0, 1)
y = np.sin(4 * np.pi * x) * np.exp(-5 * x) * 120
fig, ax = plt.subplots()
# plot only the outline of the polygon, and capture the result
poly, = ax.fill(x, y, facecolor='none')
# get the extent of the axes
xmin, xmax = ax.get_xlim()
ymin, ymax = ax.get_ylim()
# create a dummy image
img_data = np.arange(ymin,ymax,(ymax-ymin)/100.)
img_data = img_data.reshape(img_data.size,1)
# plot and clip the image
im = ax.imshow(img_data, aspect='auto', origin='lower', cmap=plt.cm.Reds_r, extent=[xmin,xmax,ymin,ymax], vmin=y.min(), vmax=30.)
im.set_clip_path(poly)
为图像提供一个范围,该范围基本上将其沿整个轴拉伸.然后clip_path使其仅显示绘制 fill
多边形的位置.
The image is given an extent which basically stretches it over the entire axes. Then the clip_path makes it only showup where the fill
polygon is drawn.
这篇关于在图表下设置颜色图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!