Cup*_*tor 11 python matplotlib
正如在此示例代码中所见,由于 0 在频谱中的某个位置,因此很难追踪哪些点是负的,哪些是正的。虽然我的真实情节更连续,但我想知道是否有办法在这些情节图中分离负值和正值;例如,我如何为正值和负值使用两种不同的颜色光谱。
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()

编辑 在@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()
我仍然不知道如何区分零!

好的,以备将来参考。正如@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()

| 归档时间: | 
 | 
| 查看次数: | 9960 次 | 
| 最近记录: |