matplotlib bwr-colormap,总是以零为中心 [英] matplotlib bwr-colormap, always centered on zero

查看:740
本文介绍了matplotlib bwr-colormap,总是以零为中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图绘制一个具有正数和负数的矩阵。数字将在从-1到1的间隔内,但不在完整范围。数字有时可以在-0.2到+0.8的范围内(见下面的代码)。
我想使用bwr-colormap(蓝色 - >白色 - 红色),这样零总是以白色编码。 -1应该在最暗的蓝色中进行颜色编码,而+1应该在最暗的可能的红色中进行颜色编码。这里有一个例子,两个图只能通过它们的颜色条来区分。

I am trying to plot a matrix with positive and negative numbers. The numbers will be in an interval from -1 to 1 but not at the complete range. Numbers could sometimes be in the range from -0.2 to +0.8 for example (See code below). I want to use the bwr-colormap (blue -> white - red) such that zero is always color-coded in white. -1 should be colorcoded in the darkest possible blue and +1 should be colorcoded in the darkest possible red. Here comes an example, where both plots are only distinguishable by their colorbar.

import numpy
from matplotlib import pyplot as plt

# some arbitrary data to plot
x = numpy.linspace(0, 2*numpy.pi, 30)
y = numpy.linspace(0, 2*numpy.pi, 20)
[X, Y] = numpy.meshgrid(x, y)
Z = numpy.sin(X)*numpy.cos(Y)

fig = plt.figure()
plt.ion()
plt.set_cmap('bwr') # a good start: blue to white to red colormap

# a plot ranging from -1 to 1, hence the value 0 (the average) is colorcoded in white
ax = fig.add_subplot(1, 2, 1)
plt.pcolor(X, Y, Z)
plt.colorbar()

# a plot ranging from -0.2 to 0.8 hence 0.3 (the average) is colorcoded in white
ax = fig.add_subplot(1, 2, 2)
plt.pcolor(X, Y, Z*0.5 + 0.3)   # rescaled Z-Data
plt.colorbar()

代码可以在这里看到:

The figure created by this code can be seen here:

如上所述,我正在寻找一种方法来总是用相同的颜色对值进行颜色编码,其中-1:深蓝色,0:白色,+1:暗红色。这是一个单线,我失去了一些东西,还是我自己为这个写的东西?

As stated above, i am looking for a way to always color-code the values with the same colors, where -1: dark blue, 0: white, +1: dark red. Is this a one-liner and i am missing something or do i have to write something myself for this?

编辑:
挖一点时间后发现一个令人满意的答案,而不是触摸色彩映射,而是使用可选输入 pcolor (见下文)。
仍然,我不会删除这个问题,因为我找不到一个答案,直到我发布了这个问题,并点击相关问题/答案。另一方面,我不介意如果它被删除,因为如果正在寻找正确的关键字,可以在别的地方找到这个问题的答案。

After digging a little bit longer i found a satisfying answer myself, not touching the colormap but rather using optional inputs to pcolor (see below). Still, I will not delete the question as i could not find an answer on SO until i posted this question and clicked on the related questions/answers. On the other hand, i wouldn't mind if it got deleted, as answers to exactly this question can be found elsewhere if one is looking for the right keywords.

推荐答案

显然,我在挖了一会儿后自己找到了答案。 pcolor 提供可选输入 vmax vmin 。如果我将它们分别设置为-1和1,它完全解决了问题。颜色编码然后似乎是相对于vmin和vmax,而不是数据的最小和最大,这是绘制的。因此,将绘图命令(和注释)更改为

Apparently, I found the answer myself after digging a little longer. pcolor offers the optional input vmax and vmin. If I set them to -1 and 1 respectively, it exactly solves the problem. The colorcoding then seems to be relative to vmin and vmax, not to the min and max of the data, which is plotted. So changing the plot command (and comments) to

# a plot ranging from -1 to 1, where the value 0 is colorcoded in white
ax = fig.add_subplot(1, 2, 1)
plt.pcolor(X, Y, Z, vmin=-1, vmax=1) # vmin, vmax not needed here
plt.colorbar()

# a plot ranging from -0.2 to 0.8, where the value 0 is colorcoded in white
ax = fig.add_subplot(1, 2, 2)
plt.pcolor(X, Y, Z*0.5 + 0.3, vmin=-1, vmax=1)   # rescaled Z-Data
plt.colorbar()

它会产生一个数字,因为我需要它:

It produces a figure as I need it:

因此,设置 vmin = -1,vmax = 1 不必更改色彩映射本身上的内容。

So, setting vmin=-1, vmax=1 does the job, i do not have to change stuff on the colormap itself.

这篇关于matplotlib bwr-colormap,总是以零为中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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