区分正值和负值的彩色绘图

2024-05-12 19:00:57 发布

您现在位置:Python中文网/ 问答频道 /正文

正如我们在这个示例代码中看到的,因为0在光谱中的某个位置,所以很难跟踪哪些点是负的,哪些是正的。虽然我的真实情节更为连续,但我想知道是否有办法在这些clorplot中分离负数和后值;例如,我如何使用两种不同的颜色光谱来表示正数和负数。

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

enter image description here

编辑 在@MultiVAC的帮助下,在寻找解决方案时,我遇到了this

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

我还是不知道怎么区分零!

enter image description here


Tags: thefromimportnumpynormmatplotlibasnp
2条回答

好的,供以后参考。正如@tcaswell建议的那样,我使用了发散图作为其中的一部分。你可以查看上面的链接。

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

enter image description here

matplotlib文档页面上有很多关于自定义分段颜色条的示例资料

例如

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

编辑:

据我所知,这可能是您所要寻找的最佳示例:

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

相关问题 更多 >