基于y值的Matplotlib颜色渐变 [英] Matplotlib Color Gradient based on y-value

查看:38
本文介绍了基于y值的Matplotlib颜色渐变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过热图绘制一个切口",即基于y值(在一个-0.5,0.5 的范围.

I would like to plot a "cut" through a heat map, i.e. apply a color gradient to my plt.plot(x,y) based on the y-values (which are in a range of -0.5,0.5.

有什么建议吗?我试图通过使用散点图来解决此问题,但是颜色图似乎是单独(而不是全局)应用于每行的.

Any suggestions? I tried to workaround by using a scatter-plot, but the colormap seemed to be applied to each line individually (and not globally).

谢谢!

编辑用其他术语来描述它:我想将间隔中的每个y值映射到我的颜色图中的适当颜色.(短语切入热图"只是指将给定 x 和 y 的函数 z=f(x,y) 的 z 值映射到颜色的常用技术.)

EDIT To describe it in other terms: I would like to map each y-value in the interval to an appropriate color in my colormap. (The phrase 'cut through heat map' just refers to the commonly used technique to map the z-values of a function z=f(x,y) for given x and y to a color.)

如前所述,仅将颜色图应用于散点图似乎会将每条线的最大 y 值(因为我正在绘制多个系列)映射到白色,如附件所示.相反,我想将绘图中的全局 y=0.5 映射到白色,将 -0.5 映射到每条线的黑色.

As mentioned earlier already, just applying a colormap to a scatter-plot seems to to map the maximum y-value of each line (as i am plotting multiple series) to white, as seen in the attachment. Instead, I would like to map the global y=0.5 in my plot to white and -0.5 to black for each line.

推荐答案

原来我需要使用

norm = colors.Normalize(vmin=-min, vmax=max)

然后调用plt.scatter(x, y, c=y, norm=norm, ...).

这篇关于基于y值的Matplotlib颜色渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆