gpt4 book ai didi

matplotlib 标准颜色图用法

转载 作者:行者123 更新时间:2023-12-03 17:13:34 24 4
gpt4 key购买 nike

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

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

它产生:

enter image description here

我不明白的一点是所有其他颜色都去哪儿了?据我了解,通过指定 vmin=0 , vmax=3那么颜色条应该使用全范围 cmap就像这张图片:

enter image description here

这是在没有给出 vmin 的情况下生产的, vmaxlevels论据。所以......我在这里错过了什么?

编辑 1

响应 tom10 & tcaswell。我本来希望它像你说的那样,但是……不幸的是,事实并非如此。看看这个:
plt.contourf([[.12, .2], [.8, 3.2]], levels=[0, .1, .3, .5, 1, 3], cmap=cmap, vmin=0, vmax=3)
plt.colorbar()

和:

enter image description here

也许要澄清一下:假设我有数据并且它的重要特征在 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 颜色图)。我想说的是...如果我给 levels=[0, 1, 2, 3], vmin=0, vmax=3对于一些从 0 到 3 的数据,我希望看到给定颜色图中的所有颜色,但如果我给出 levels=[0, 0.9, 0.1, 0.11, 1, 3], vmi=0, vmax=3我希望看到给定颜色图中的所有颜色,除了映射到正确的间隔之外,我希望看到相同的颜色,而是看到一束蓝色为 0-0.11 区域着色,而一些绿色/黄色为该区域的其他部分着色。希望这使它......有点清楚。

编辑 2

即使我不提供任何信息,也会发生同样的情况 normvmin, vmax .

编辑 3

引用 tcaswell 的评论,按照现在的方式行事......至少对我来说是违反直觉的。我希望颜色在某种程度上独立于数据点。我希望颜色图中的所有颜色范围都可以使用(除非 vmin, vmax 大于/小于 levels min, max 值)。换句话说,看看这段代码我做了一段时间(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,它们不再位于蓝色区域中。 :

enter image description here

我的意思是,我知道我解决了这个问题,不久前也解决了……但我想我的问题是……为什么 matplotlib 中的默认值不是这个?我本来希望它是这样的......或者它可能只是一个配置/参数/默认情况下启用它的东西,我错过了?

最佳答案

在玩了一会儿之后,似乎这个问题的答案比我想象的要容易得多。先简单解释一下。从 matplotlib.colors 阅读有关规范化类的文档时我想……好吧,matplotlib.colors.BoundaryNorm应该用在这里!但正如您在以下示例中看到的那样,出现了问题:

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

这给出了:
enter image description here
这显然是我们不想要的!我在想……你为什么要给 BoundaryNorm 的构造函数?要使用的颜色数量?...不应该 BoundaryNorm使用颜色图的全部范围?然后它让我震惊,对上面的代码做了一点改动:
# 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)

我们得到:
enter image description here
这正是我们想要的!

或者你我们可以这样做:
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 标准颜色图用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18599625/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com