Bor*_*lik 8 python color-scheme matplotlib color-mapping
以下python代码创建包含正态分布值的矩阵的热图
import numpy as np
from matplotlib import pylab as plt
np.random.seed(123) #make sure we all have same data
m = np.random.randn(200).reshape(10, 20)
plt.imshow(m, cmap='RdYlGn', interpolation='nearest')
plt.colorbar()
Run Code Online (Sandbox Code Playgroud)
这是此代码的输出

我想通过"淡出"接近零的值来增强该图像的对比度.我可以通过使用原始数据的disigmoid缩放来轻松完成此操作,如下所示:
def disigmoidScaling(values, steepnessFactor=1, ref=None):
''' Sigmoid scaling in which values around a reference point are flattened
arround a reference point
Scaled value y is calculated as
y = sign(v - d)(1 - exp(-((x - d)/s)**2)))
where v is the original value, d is the referenc point and s is the
steepness factor
'''
if ref is None:
mn = np.min(values)
mx = np.max(values)
ref = mn + (mx - mn) / 2.0
sgn = np.sign(values - ref)
term1 = ((values - ref)/steepnessFactor) ** 2
term2 = np.exp(- term1)
term3 = 1.0 - term2
return sgn * term3
plt.imshow(disigmoidScaling(m, 4), cmap='RdYlGn', interpolation='nearest')
plt.colorbar()
Run Code Online (Sandbox Code Playgroud)
这是输出.

我对结果很满意,除了在这个版本中原始值已经换成缩放的值.
有没有办法将值非线性映射到色彩映射?
颜色图包含映射在区间 [0,1] 上的红色、绿色和蓝色值的字典。Linear Segmented Colormap类文档给出了示例
cdict = {'red': [(0.0, 0.0, 0.0),
(0.5, 1.0, 1.0),
(1.0, 1.0, 1.0)],
'green': [(0.0, 0.0, 0.0),
(0.25, 0.0, 0.0),
(0.75, 1.0, 1.0),
(1.0, 1.0, 1.0)],
'blue': [(0.0, 0.0, 0.0),
(0.5, 0.0, 0.0),
(1.0, 1.0, 1.0)]}
Run Code Online (Sandbox Code Playgroud)
“表中给定颜色的每一行都是 x、y0、y1 元组的序列。在每个序列中,x 必须从 0 单调增加到 1。对于落在 x[i] 和 x[i+ 之间的任何输入值 z 1],给定颜色的输出值将在 y1[i] 和 y0[i+1] 之间线性插值:"
颜色图RdYlGn对每种颜色有 11 个 x 值,从 0 到 1.0,步长为 0.1。您可以cdict通过调用获取值
plt.cm.RdYlGn._segmentdata
Run Code Online (Sandbox Code Playgroud)
然后,您可以将 x 值更改为您想要的任何步骤(只要它们单调递增且范围从 0 到 1),并通过调用新matplotlib.colors.LinearSegmentedColormap的cdict. Matplotlib Cookbook中有几个很好的例子。
| 归档时间: |
|
| 查看次数: |
5296 次 |
| 最近记录: |