Python Matplotlib更改超过颜色条范围的值的默认颜色 [英] Python matplotlib change default color for values exceeding colorbar range

查看:1128
本文介绍了Python Matplotlib更改超过颜色条范围的值的默认颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用颜色填充网格时,例如在pyplot中使用contourf时,我需要找到一种方法来更改pyplot用于填充超出颜色栏指定范围的数据的颜色.我希望有一个静态色条,它不会自动更改其范围以适合数据的最大值/最小值,因此不可避免地会出现偶尔超出其界限的极值,因此需要为这些值指定颜色.

When filling a grid with color such as when using contourf in pyplot, I need to find a way to change the color that pyplot uses to fill data that exceed the specified range of the colorbar. I wish to have a static colorbar that does not automatically change its range to fit the max/min of the data, so having occasional extreme values that exceed its bounds is inevitable, and colors need to be specified for such values.

超出颜色条范围的值的默认颜色是白色,如果颜色图的最终颜色不为白色,则该颜色可能会与周围数据发生明显冲突.示例图片如下所示-当值超出颜色条的负范围时,请注意白色填充:

The default color for values exceeding the bounds of the colorbar is white, which can glaringly clash with the surrounding data if the colormap does not have white as its end colors. Example image is shown below - notice the white fill when values exceed the negative range of the colorbar:

我相信有一种方法可以指定使用rcParams在每个边界处使用的颜色,但是我无法在任何地方找到有关此颜色的信息.

I believe there is a way to specify which color to use at each bound if they are exceeded by using rcParams, but I have not been able to find information on this anywhere.

任何帮助将不胜感激.

推荐答案

可以使用颜色图的set_overset_under方法设置边界颜色;请参阅文档.创建色图时,您需要指定这些值.我没有看到任何matplotlibrc设置来为此设置默认值.您可能还想在matplotlib邮件列表上询问.

The out-of-bounds colors can be set using the set_over and set_under methods of the colormap; see the documentation. You'll need to specify these values when you create your colormap. I don't see any matplotlibrc setting to set the default for this, though. You might also want to ask on the matplotlib mailing list.

我知道发生了什么事.您描述的白色区域不超出颜色范围的限制.它只是轴的空白背景.因为您仅绘制某些级别,所以根本不会绘制该范围之外的任何级别,而将那些区域留为空白.要获得想要的东西,请执行以下操作:

I see what is going on. The white area you describe is not beyond the limits of the color range. It is simply the blank background of the axes. Because you are only plotting certain levels, any levels outside that range will not be plotted at all, leaving those areas blank. To get what you want, do this:

cs = pyplot.contourf(x,y,z,levels=np.arange(50, 220, 20), cmap=pyplot.cm.jet, extend="both")
cs.cmap.set_under('k')
cs.set_clim(50, 210)
cb = pyplot.colorbar(cs)

"extend"参数是关键;它告诉contourf继续绘制所有轮廓,但是将所有超出给定范围的图像折叠为太大"和太小"类别. cs.set_clim调用对于解决我在进行调试时在contourf中发现的异常是必要的;由于某些原因,当您使用extend时,它会操纵数据限制,因此我们需要将其重置为我们想要的值.

The "extend" argument is the key; it tells contourf to go ahead and plot all contours, but collapse all outside the given range into "too big" and "too small" categories. The cs.set_clim call is necessary to work around an oddity I discovered in contourf while debugging this; for some reason when you use extend, it manipulates the data limits, so we need to reset them back to what we want them to be.

此外,就样式而言,您不应该执行Colormap.set_under(cmap,color='k')之类的事情.这是在调用类方法并显式传递实例,这是一种奇怪的方法.只需cmap.set_under(color="k").

Also, just as a matter of style, you shouldn't be doing things like Colormap.set_under(cmap,color='k'). This is calling the class method and explicitly passing the instance in, which is an odd way to do it. Just do cmap.set_under(color="k").

这篇关于Python Matplotlib更改超过颜色条范围的值的默认颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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