简体   繁体   中英

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). 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.

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. 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:

在此处输入图片说明

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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