简体   繁体   English

matplotlib中轮廓和轮廓f之间的颜色差异

[英]Color differences between contour and contourf in matplotlib

I'm trying to produce a plot which uses both contour and contourf where they both use the same colormap, plotting the same data. 我正在尝试制作一个使用轮廓和轮廓线的图,它们都使用相同的颜色图,绘制相同的数据。 However, contourf is only used to plot data which is 'significant' in some way (using a masked array). 但是,contourf仅用于以某种方式(使用屏蔽数组)绘制“重要”数据。 Meanwhile, contour is used to plot all the data. 同时,轮廓线用于绘制所有数据。 The aim is to produce a plot where all the information is available, but the eye is drawn to the more important areas. 目的是绘制一个可以获取所有信息的图表,但将注意力吸引到更重要的区域。

I nearly have this working as I would like, but I am finding that the color of the contour lines is slightly different from the color of the filled contours from contourf. 我几乎可以按照自己的意愿进行这项工作,但是我发现轮廓线的颜色与Contourf中填充轮廓的颜色略有不同。

I'm guessing that the differences come from the fact that either the contour colors are actually half-way between the contour colors (which would make sense, as the contour lines are defined at a value, eg 1, 2, etc, and the filled contours are between 1 and 2, ie with a 'value' of 1.5 etc). 我猜这是因为轮廓颜色实际上是轮廓颜色之间的一半(这很有意义,因为轮廓线定义为一个值,例如1、2等)和填充轮廓在1和2之间,即“值”为1.5等)。

I am defining my colormap as 我将颜色图定义为

cmap = cm.coolwarm
cnorm=clrs.Normalize(cmap,clip=False)
cmap.set_under(color=cmap(0.0),alpha=1.0)
cmap.set_over(color=cmap(1.0),alpha=1.0)

my contour levels, used for both contour and contourf are 我用于轮廓和轮廓线f的轮廓线水平是

clevs = [-3.,-2.,-1.,1.,2.,3.]

The contour lines are plotted as 等高线绘制为

cplot=map.contour(x,y,diff,clevs,\
                      cmap=cmap,\
                      norm=cnorm,\
                      extend='both')

and the filled contours are plotted as 填充的轮廓绘制为

cplot=map.contourf(x,y,true_mask,clevs,cmap=cmap,\
                       norm=cnorm,
                       extend='both')

Is there a straight-forward way to have the colors of the contour lines 'match' those of the filled contours, ie the line at 1 is the color of the 1-2 filled contour, the line at 2 is the color of the 2-3 filled contour, the line at -1 have the color of the -2--1 filled contour etc.? 是否有一种简单的方法来使轮廓线的颜色“匹配”填充轮廓的颜色,即1处的线是1-2个填充轮廓的颜色,2处的线是2的颜色-3填充轮廓,-1处的线具有-2--1填充轮廓等的颜色?

Many thanks for your help. 非常感谢您的帮助。

I think that one possible solution here is to create a new colormap for the call to contour. 我认为这里一种可能的解决方案是为轮廓调用创建新的颜色图。 If the original colormap is defined as 如果原始色彩图定义为

cmap = matplotlib.cm.coolwarm
cnorm=matplotlib.colors.Normalize(cmap,clip=False)
cmap.set_under(color=cmap(0.0),alpha=1.0)
cmap.set_over(color=cmap(1.0),alpha=1.0)

and the contour levels to plot as 和等高线图绘制为

clevs = [-3.,-2.,-1.,1.,2.,3.]

then the new colormap can be created by 然后可以通过创建新的颜色图

cw=matplotlib.cm.get_cmap('coolwarm',8*(len(clevs)+1))
cw_vals=cw(np.arange(8*(len(clevs)+1)))
new_cw_vals=np.zeros([len(clevs),cw_vals.shape[1]],dtype=np.float128)
new_cw_vals_t = np.float128(cw_vals[4::8,:])
new_cw_vals_b = np.float128(cw_vals[12::8,:])
for i in np.arange(new_cw_vals.shape[0]):
    if clevs[i] < 0.0:
        new_cw_vals[i,:]=np.float32(new_cw_vals_t[i,:])
    else:
        new_cw_vals[i,:]=np.float32(new_cw_vals_b[i,:])
newcmap = matplotlib.colors.LinearSegmentedColormap.from_list("newcw", new_cw_vals)
newcnorm=matplotlib.colors.Normalize(newcmap,clip=False)

I had to put a shift in to allow for the fact that I'm not plotting the 0 line. 我不得不进行调整,以考虑到我没有绘制0线。

which is then used in the call to contour 然后在轮廓调用中使用

cplot=map.contour(x,y,diff,clevs,
                      cmap=newcmap,
                      norm=newcnorm,
                      vmin=vmin,vmax=vmax)

Essentially I am creating a colormap with 8 times the number of points, and then picking out the mid-points. 本质上,我创建的色图是点数的8倍,然后挑选出中点。 It isn't quite perfect though, the colors are ever-so-slightly off from the colors from contourf. 不过,它还不是很完美,颜色与Contourf的颜色有些许差异。 This may be down to rounding differences (the colormap values seem to be float32). 这可能归结为舍入差异(颜色图值似乎是float32)。 It is also a bit specific to the values used in clevs, although it could easily be changed for other values. 尽管它可以很容易地更改为其他值,但它还是特定于clevs中使用的值。

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

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