您的链接为色图提供了很好的解决方案。我做了一些编辑,但其中包含所有必要内容。您需要为非线性色彩图选择一些合理的级别。我
+-4在样本的标准偏差之间使用了两个以平均值为中心的范围。通过将其更改为另一个数字,可以在两个平均值附近获得不同的颜色局部梯度。
对于颜色条,您
- 要么用线性间隔的标签使颜色非线性间隔
- 您具有带有非线性间隔标签的线性间隔颜色。
第二种方法在查看数据时可以提供更高的分辨率,看起来更好,并在下面实现:
import numpy as npimport matplotlib.pyplot as pltx = y = np.linspace(1, 10, 10)t1mean, t2mean = 2, 9sigma1, sigma2 = .3, .01t1 = np.random.normal(t1mean, sigma1, 10)t2 = np.random.normal(t2mean, sigma2, 10)class nlcmap(object): def __init__(self, cmap, levels): self.cmap = cmap self.N = cmap.N self.monochrome = self.cmap.monochrome self.levels = np.asarray(levels, dtype='float64') self._x = self.levels self.levmax = self.levels.max() self.transformed_levels = np.linspace(0.0, self.levmax, len(self.levels)) def __call__(self, xi, alpha=1.0, **kw): yi = np.interp(xi, self._x, self.transformed_levels) return self.cmap(yi / self.levmax, alpha)tmax = max(t1.max(), t2.max())#the choice of the levels depends on the data:levels = np.concatenate(( [0, tmax], np.linspace(t1mean - 4 * sigma1, t1mean + 4 * sigma1, 5), np.linspace(t2mean - 4 * sigma2, t2mean + 4 * sigma2, 5), ))levels = levels[levels <= tmax]levels.sort()cmap_nonlin = nlcmap(plt.cm.jet, levels)fig, (ax1, ax2) = plt.subplots(1, 2)ax1.scatter(x, y, edgecolors=cmap_nonlin(t1), s=15, linewidths=4)ax2.scatter(x, y, edgecolors=cmap_nonlin(t2), s=15, linewidths=4)fig.subplots_adjust(left=.25)cbar_ax = fig.add_axes([0.10, 0.15, 0.05, 0.7])#for the colorbar we map the original colormap, not the nonlinear one:sm = plt.cm.ScalarMappable(cmap=plt.cm.jet, norm=plt.Normalize(vmin=0, vmax=tmax))sm._A = []cbar = fig.colorbar(sm, cax=cbar_ax)#here we are relabel the linear colorbar ticks to match the nonlinear tickscbar.set_ticks(cmap_nonlin.transformed_levels)cbar.set_ticklabels(["%.2f" % lev for lev in levels])plt.show()
在结果中,请注意色条的刻度不是等距的:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)