简体   繁体   English

具有自定义级别的Matplotlib Contourf图的颜色图被忽略

[英]Colormap is being ignored for matplotlib contourf plot with custom levels

I am trying to create a filled contour plot in matplotlib (Win7, 1.1.0). 我正在尝试在matplotlib(Win7,1.1.0)中创建一个填充轮廓图。 I want to highlight certain values, and the levels are closer to log than linear. 我想突出显示某些值,并且级别比线性更接近对数。

There are numerous colormaps that would suit me, but my choice of cmap is ignored. 有许多适合我的颜色图,但是我对cmap的选择被忽略了。

Do I need to create a custom "normalize"? 我需要创建自定义的“规范化”吗? If so is each contour colored according to its edge value and then filled with the same color to the next lower value? 如果是,每个轮廓是否根据其边缘值着色,然后用相同的颜色填充到下一个较低的值? Why is the symptom of this to ignore my color map ... is this some exception during construction that is being caught and my request is being silently ignored? 为什么这样的症状会忽略我的色彩映射表……这是在构造过程中被捕获的一个异常,而我的请求却被默默地忽略了吗?

My original data had missing values. 我的原始数据缺少值。 I have played with making thise nan, large and small ... in each case I have tried masking them and not masking the "outside" values. 我一直在做这个大小的游戏,在每种情况下,我都尝试屏蔽它们而不是屏蔽“外部”值。 I have also tried all permutations using the default levels and norm. 我还尝试了使用默认级别和规范的所有排列。

lev = [0.1,0.2,0.5,1.0,2.0,4.0,8.0,16.0,32.0]
norml = colors.normalize(0,32)
cs = plt.contourf(x,z,data,cmap=cm.gray, levels=lev, norm = norml)

I hope this snippet is sufficient to at least start the conversation. 我希望此片段足以至少开始对话。

Thanks, Eli 谢谢,以利

If I understood you correctly, you need to rescale your data to colors using your levels as the basis rather than default linear scaling. 如果我对您的理解正确,则需要使用色阶作为基础而不是默认的线性缩放将数据重新缩放为颜色。 If that's right, then you need to use colors.BoundaryNorm as the norm factor. 如果正确,那么您需要使用colors.BoundaryNorm作为标准因子。 Consider the following example: 考虑以下示例:

x = np.arange(0,8,0.1)
y = np.arange(0,8,0.1)
z = (x[:,None]-4) ** 2 + (y[None,:]-4) ** 2

lev = [0.1,0.2,0.5,1.0,2.0,4.0,8.0,16.0,32.0]
norml = colors.BoundaryNorm(lev, 256)
cs = plt.contourf(x, y, z, cmap = cm.jet, levels = lev, norm = norml)
plt.show()

This yields 这产生

在此处输入图片说明

Compare it to default Normalize behaviour: 将其与默认的Normalize行为进行比较:

在此处输入图片说明

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM