简体   繁体   English

使用自定义cmap的matplotlib散点图-颜色不正确

[英]matplotlib scatter plot with custom cmap - colors not right

So I'm following a pattern to create a custom color map and cbar for my scatter plot. 因此,我遵循一种模式为散点图创建自定义颜色图和cbar。 I'm creating 4 subplots each covering a different range of a parameter that is used to give the dots their color. 我正在创建4个子图,每个子图覆盖用于赋予点颜色的参数的不同范围。 The values for this parameter range from 1e-10 to 1.0. 该参数的取值范围是1e-10到1.0。

I do the following: 我执行以下操作:

cmap = plt.cm.jet
# extract all colors from the .jet map
cmaplist = [cmap(i) for i in range(cmap.N)]
# force the first color entry to be grey
cmaplist[0] = (.5,.5,.5,1.0)
# create the new map
cmap = cmap.from_list('Custom cmap', cmaplist, cmap.N)

# define the bins and normalize
bounds = np.linspace(-10,0,11)
norm   = mpl.colors.BoundaryNorm(bounds, cmap.N)
...
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex='col', sharey='row', figsize=(20,20))
...
ax1.scatter(xcoord[rng1], ycoord[rng1], s=massFilt[rng1]/dotNorm,
                    c=np.ma.log10(Zfilt[rng1]), cmap=cmap)
ax2.scatter(xcoord[rng2],ycoord[rng2], s=massFilt[rng2]/dotNorm,
                    c=np.ma.log10(Zfilt[rng2]), cmap=cmap)
ax3.scatter(xcoord[rng3], ycoord[rng3], s=massFilt[rng3]/dotNorm,
                    c=np.ma.log10(Zfilt[rng3]), cmap=cmap)
ax4.scatter(xcoord[rng4], ycoord[rng4], s=massFilt[rng4]/dotNorm,
                    c=np.ma.log10(Zfilt[rng4]), cmap=cmap)

where the rngs are defined as 其中rng被定义为

rng1 = (Zfilt < 2.e-7)
rng2 = ((Zfilt >= 2.e-7) & (Zfilt < 1.e-4))
rng3 = ((Zfilt >= 1.e-4) & (Zfilt < 1.e-2))
rng4 = (Zfilt >= 1.e-2)

So the first panel includes only those less than 2e-7 and the points are all colored correctly... I've verified that the filter is working correctly: for instance Zfilt[rng4] only contains points where Zfilt > 1e-2 ... But somehow I'm seeing colors in rng2 and rng3 that should only be used for rng1 !!?? 因此,第一个面板仅包括小于2e-7的那些点,并且所有点均正确着色...我已经验证了过滤器是否正常工作:例如Zfilt [rng4]仅包含Zfilt> 1e-2的点。 但是,我以某种方式在rng2和rng3中看到只能用于rng1的颜色 See attached. 见附件。

Any ideas? 有任何想法吗? What am I doing wrong? 我究竟做错了什么?

面板2(右上方)中的所有点都小于1e-4,应为绿色,蓝色或灰色...不是红色?

Here's a sample of Zfilt[rng2] - 这是Zfilt [rng2]的示例-

Z values rng2  [-4.23451696 -4.35974369 -5.18479833 -6.17304647 -4.48839191 -5.16774006
 -4.12047222 -6.11491263 -5.81392662 -4.6491248  -4.75038775 -5.06640103
 -4.20821705 -4.12556725 -4.58661378 -4.17023495 -5.40845781 -4.54981553
 -5.82830048 -4.11185471 -4.43155534 -6.17025186 -4.88154584 -5.00024704
 -4.13626926 -5.57797731 -6.0617742  -5.33182163 -5.44963247 -5.88409509
 -6.16903327 -5.70808154 -4.88578943 -4.00873256 -4.1457824  -4.45174817
 -5.43829583 -4.32470978 -4.11634754 -5.1141915  -5.13310282 -4.15469421
...

scatter() has no idea about the supposed range of your data, so it applies the color map for the full range it sees, that is from the minimum to the maximum value of the supplied array. scatter()并不了解数据的假定范围,因此它将颜色映射应用于所看到的整个范围,即从所提供数组的最小值到最大值。 Since you want a different behavior (same range for all the subplots, regardless of the data), you need to specify the range explicitly via vmin and vmax arguments: 由于您想要不同的行为(所有子图的范围都相同,而不管数据如何),因此需要通过vminvmax参数显式指定范围:

ax1.scatter(xcoord[rng1], ycoord[rng1], s=massFilt[rng1]/dotNorm,
                c=np.ma.log10(Zfilt[rng1]), cmap=cmap,
                vmin=-10, vmax=0)
ax2.scatter(xcoord[rng2],ycoord[rng2], s=massFilt[rng2]/dotNorm,
                c=np.ma.log10(Zfilt[rng2]), cmap=cmap,
                vmin=-10, vmax=0)
# etc

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

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