matplotlib标准颜色图用法 [英] matplotlib standard colormap usage

查看:48
本文介绍了matplotlib标准颜色图用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用matplotlib 1.3.0,并且具有以下内容:

I'm using matplotlib 1.3.0 and I have the following:

import matplotlib.pyplot as plt
cmap = plt.cm.jet
plt.contourf([[.12, .2], [.8, 2]], levels=[0, .1, .3, .5, 1, 3], cmap=cmap, vmin=0, vmax=3)
plt.colorbar()

产生:

我不明白的是,所有其他颜色都去了哪里?据我了解,通过指定 vmin = 0 vmax = 3 ,然后颜色栏应使用完整的 cmap 范围,如下图所示:

The bit that I don't understand is where did all of the other colors go? As I understand, by specifying vmin=0, vmax=3 then the color bar should use the full range of cmap like in this image:

,它在没有给出 vmin vmax levels 参数的情况下产生.所以...我在这里想念什么?

which is produced without giving the vmin, vmax and levels arguments. So... what am I missing here?

编辑1

响应tom10&tcaswell.我本来希望像您说的那样,但是...不幸的是,事实并非如此.看看这个:

In response to tom10 & tcaswell. I would have expected it to be as you say, but... unfortunately it's not. Take a look at this:

plt.contourf([[.12, .2], [.8, 3.2]], levels=[0, .1, .3, .5, 1, 3], cmap=cmap, vmin=0, vmax=3)
plt.colorbar()

具有:

也许可以澄清一下:说我有数据,它的重要特征在0.1左右,但大约3个.所以我给它一个 levels = [0,0.005,0.075,0.1,0.125,0.15,0.2,1,2.5,2.75,3,3.25] vmin = 0,vmax = 3.25.现在,我希望看到所有颜色,但是所有重要的数据点0.005至0.125最终都位于蓝色区域(通过使用标准的 plt.cm.jet 颜色图).我想说的是...如果我给出从0到3的某些数据的 levels = [0,1,2,3],vmin = 0,vmax = 3 期望看到给定颜色图中的所有颜色,但是如果我给 levels = [0,0.9,0.1,0.11,1,3],vmi = 0,vmax = 3 ,我希望同样,要查看给定颜色图中的所有颜色(除了映射到正确的间隔外),相反,我看到一束蓝色为0-0.11区域着色,而一些绿色/黄色为该区域的其他部分着色.希望这可以使它...一点点清楚.

Maybe to clarify this a bit: say I have data and the important features of it are around 0.1, but there are some around 3 let's say. So I give it a levels=[0, 0.005, 0.075, 0.1, 0.125, 0.15, 0.2, 1, 2.5, 2.75, 3, 3.25] and vmin=0, vmax=3.25. Now I would expect to see the full range of colors, but instead all of the important data-points 0.005 to 0.125 end up in the blue region (by using the standard plt.cm.jet color map). What I'm saying I guess is... if I give levels=[0, 1, 2, 3], vmin=0, vmax=3 for some data that goes from 0 to 3 I expect to see all the colors in the given color map, but if I give levels=[0, 0.9, 0.1, 0.11, 1, 3], vmi=0, vmax=3 I would expect the same, to see all the colors in the given color map, except mapped to the right intervals, instead I see the bunch of blues coloring the 0-0.11 region and some green / yellow coloring the other part of the region. Hope this makes it... a bit clear.

编辑2

即使我不给出任何 norm vmin,vmax ,也会发生同样的情况.

The same happens even if I don't give any norm or vmin, vmax.

编辑3

参考tcaswell的评论,表现方式……对我而言至少是违反直觉的.我希望颜色在某种程度上与数据点无关.我希望可以一​​直使用颜色表中的所有颜色(除非 vmin,vmax 大于/小于 levels 最小,最大值)).换句话说,看一下这段代码,我做了一段时间(Python 3):

Referring to tcaswell's comment, behaving the way it is... for me at least is counter-intuitive. I expected that the color would be independent of the data-points in a way. I would expect that the full range of colors from the colormap would be used all the time (except when the vmin, vmax are larger/smaller than the levels min, max values). In other words, looking at this code I did a while back (Python 3):

import matplotlib.colors as mc
def addNorm(cmapData):
    cmapData['norm'] = mc.BoundaryNorm(cmapData['bounds'], cmapData['cmap'].N)
    return True
def discretize(cmap, bounds):
    resCmap = {}
    resCmap['cmap'] = mc.ListedColormap( \
        [cmap(i/len(bounds[1:])) for i in range(len(bounds[1:]))]
    )
    resCmap['bounds'] = bounds
    addNorm(resCmap)
    return resCmap

然后将其用作:

levels = [0, .1, .3, .5, 1, 3]
cmapData = discretize(plt.cm.jet, bounds=levels)
plt.contourf([[.12, .2], [.8, 3.2]], levels=levels, cmap=cmapData['cmap'], norm=cmapData['norm'])
plt.colorbar()

提供了一个图,您可以在其中实际区分特征(0.1-0.5),即通过使用上述方法和 plt.cm.jet ,它们不再位于蓝色区域:

which gives the plot where you can actually distinguish the features (0.1-0.5), i.e. they are no longer in the blue region by using the above method with plt.cm.jet:

我的意思是,我知道我也解决了这个问题,但不久前...但是我想我的问题是... matplotlib中的默认值不是吗?我本来希望是这种方式...或者可能只是配置/参数/某些我默认缺少的启用此功能的东西?

I mean, I know I solved this, and a while back too... but my question I guess is... how come the default in matplotlib is not this? I would have expected it to be this way... or maybe is it just a configuration / argument / something to enable this by default that I'm missing?

推荐答案

经过一番摸索,似乎这个问题的答案比我想象的要容易得多.首先只是一些解释.当我从 matplotlib.colors 阅读有关规范化类的文档时,我想......在这里应该使用 matplotlib.colors.BoundaryNorm !但是在下面的示例中您会发现有些问题:

After playing around a bit it seems that the answer to this question is way easier than I ever thought. Just some explanation first. While reading the documentation on the normalizing classes from matplotlib.colors I figured... well, matplotlib.colors.BoundaryNorm should be used here! but something is wrong as you can see in the following example:

import matplotlib.pyplot as plt
import matplotlib.colors as mc
levels = [0, .1, .3, .5, 1, 3]
norm = mc.BoundaryNorm(levels, len(levels)-1)
plt.contourf([[.12, .2], [.8, 2]], levels=levels, norm=norm)
plt.colorbar()
plt.show()

给出以下内容:这显然是我们不想要的!我在想...您为什么要给 BoundaryNorm 的构造函数使用多少种颜色?... BoundaryNorm 不应该使用全部范围吗?的颜色图?然后它令我震惊,对上面的代码做了一点改动:

which gives this: and this is obviously something we don't want! And I was thinking... why would you have to give to the constructor of BoundaryNorm the number of colors to use?... Shouldn't BoundaryNorm use the full extent of the colormap? And then it struck me, with just a little change to the code above:

# use here 256 instead of len(levels)-1 becuase
# as it's mentioned in the documentation for the
# colormaps, the default colormaps use 256 colors in their
# definition: print(plt.cm.jet.N) for example
norm = mc.BoundaryNorm(levels, 256)

,我们得到:这正是我们想要的!

and we get: which is exactly what we want!

或者您可以这样做:

cmap = # user define cmap
norm = mc.BoundaryNorm(levels, cmap.N)
# which is I guess a little bit more programatically (is this a word?!) correct

这篇关于matplotlib标准颜色图用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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