简体   繁体   English

区分正值和负值的颜色图

[英]Colorplot that distinguishes between positive and negative values

As one can see in this sample code since 0 is somewhere in the spectrum it is hard to trace which points are negative and which are positive.正如在此示例代码中所见,由于 0 在频谱中的某个位置,因此很难追踪哪些点是负的,哪些是正的。 Although my real plot is more contiguous I wonder if there is a way to seperate negative and postivie values in these clorplots;虽然我的真实情节更连续,但我想知道是否有办法在这些情节图中分离负值和正值; for example how can I use two different spectrum of colours for positive and negative values.例如,我如何为正值和负值使用两种不同的颜色光谱。

import numpy as np
from matplotlib import pyplot as plt
a=np.random.randn(2500).reshape((50,50))
plt.imshow(a,interpolation='none')
plt.colorbar()
plt.show()

在此处输入图片说明

EDIT With the help of @MultiVAC and looking for solutions I came across this .编辑在@MultiVAC 的帮助下,我在寻找解决方案时遇到了这个问题

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.colors import BoundaryNorm
a=np.random.randn(2500).reshape((50,50))

# define the colormap
cmap = plt.cm.jet
# extract all colors from the .jet map
cmaplist = [cmap(i) for i in range(cmap.N)]
# create the new map
cmap = cmap.from_list('Custom cmap', cmaplist, cmap.N)

# define the bins and normalize
bounds = np.linspace(np.min(a),np.max(a),5)
norm = BoundaryNorm(bounds, cmap.N)

plt.imshow(a,interpolation='none',norm=norm,cmap=cmap)
plt.colorbar()
plt.show()

Still I don't know how to differentiate zero!我仍然不知道如何区分零!

在此处输入图片说明

Ok for the future reference.好的,以备将来参考。 I used diverging maps as part of it as @tcaswell suggested.正如@tcaswell 建议的那样,我使用发散图作为其中的一部分。 You can look to the above links.你可以看看上面的链接。

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.colors import BoundaryNorm
a=np.random.randn(2500).reshape((50,50))

# define the colormap
cmap = plt.get_cmap('PuOr')

# extract all colors from the .jet map
cmaplist = [cmap(i) for i in range(cmap.N)]
# create the new map
cmap = cmap.from_list('Custom cmap', cmaplist, cmap.N)

# define the bins and normalize and forcing 0 to be part of the colorbar!
bounds = np.arange(np.min(a),np.max(a),.5)
idx=np.searchsorted(bounds,0)
bounds=np.insert(bounds,idx,0)
norm = BoundaryNorm(bounds, cmap.N)

plt.imshow(a,interpolation='none',norm=norm,cmap=cmap)
plt.colorbar()
plt.show()

在此处输入图片说明

there is a lot of good example material on simple self-defined segmented color bars on the matplotlib documentation pages matplotlib 文档页面上的简单自定义分段颜色条上有很多很好的示例材料

for instance例如

http://matplotlib.org/examples/api/colorbar_only.html http://matplotlib.org/examples/pylab_examples/contourf_demo.html http://matplotlib.org/examples/api/colorbar_only.html http://matplotlib.org/examples/pylab_examples/contourf_demo.html

EDIT:编辑:

from what I understand, this might be the perfect example for what you are looking for:据我了解,这可能是您正在寻找的完美示例:

http://matplotlib.org/examples/pylab_examples/custom_cmap.html http://matplotlib.org/examples/pylab_examples/custom_cmap.html

I arrived at this thread looking for something like what I've written below, hopefully others find it helpful.我来到这个线程寻找类似于我在下面写的内容,希望其他人觉得它有帮助。

import matplotlib.colors as colors
from matplotlib import cm
import numpy as np
import seaborn as sns

with sns.axes_style('whitegrid'):
    rand_normal_y = np.random.randn(1000)
    x = np.arange(0,1000, 1)
    norm = colors.CenteredNorm()
    rand_normal_y_norm = norm(rand_normal_y)
    cmap = cm.coolwarm(rand_normal_y_norm)
    sns.scatterplot(x = x, y = rand_normal_y , c=cmap,  )
    plt.plot(np.linspace(0,1000, 1000), np.repeat(0, 1000), color = 'black', ls = "-")

在此处输入图片说明

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

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