【发布时间】:2011-06-27 11:59:47
【问题描述】:
以下 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()
这是这段代码的输出
我想通过“淡出”接近零的值来增强此图像的对比度。 我可以通过使用原始数据的二乙型缩放轻松做到这一点,如下所示:
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()
这是输出。
我对结果很满意,除了在这个版本中原来的 值已交换为按比例缩放的值。
有没有办法将值非线性映射到颜色图?
【问题讨论】:
-
这个问题在这里可能也很有趣:stackoverflow.com/questions/46038206/…
标签: python matplotlib color-scheme color-mapping