色彩图的非线性缩放以增强对比度
以下 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()
这是输出。
我对结果很满意,除了这个版本中的原始版本 值已交换为缩放值。
有没有办法执行值到颜色图的非线性映射?
The following python code creates a heatmap of a matrix that contains normally distributed values
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()
This is the output of this code
I would like to enhance the contrast of this image by "fading out" the values close to zero.
I can easily do this by using disigmoid scaling of the original data as follows:
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()
Here is the output.
I'm pleased with the result, except the fact that in this version the original
values have been exchanged for scaled ones.
Is there a way to perform a non-linear mapping of values to colormap?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
颜色图包含映射在区间 [0,1] 上的红色、绿色和蓝色值的字典。 线性分段颜色图 类文档给出了示例
“每一行给定颜色的表是 x、y0、y1 元组的序列。在每个序列中,x 必须从 0 单调增加到 1。对于任何输入值 z。落在 x[i] 和 x[i+1] 之间,给定颜色的输出值将在 y1[i] 和 y0[i+1] 之间线性插值:”
RdYlGn
颜色图具有每种颜色有 11 个 x 值,从 0 到 1.0,步长为 0.1。您可以通过调用获取cdict
值,然后您可以将 x 值更改为您想要的任何步骤(只要它们单调递增且范围从 0 到 1),并通过调用 <新的
cdict
上的 code>matplotlib.colors.LinearSegmentedColormap 。 Matplotlib Cookbook 中有几个很好的例子。A colormap contains a dictionary of red, green and blue values mapped over the interval [0,1]. The Linear Segmented Colormap class docs give the example
"Each row in the table for a given color is a sequence of x, y0, y1 tuples. In each sequence, x must increase monotonically from 0 to 1. For any input value z falling between x[i] and x[i+1], the output value of a given color will be linearly interpolated between y1[i] and y0[i+1]:"
The
RdYlGn
colormap has 11 x values for each color going from 0 to 1.0 in steps of 0.1. You can get thecdict
values by callingYou can then change the x values to whatever steps you want (as long as they are monotonically increasing and range from 0 to 1) and get a new colormap by calling
matplotlib.colors.LinearSegmentedColormap
on your newcdict
. There are several great examples of this in the Matplotlib Cookbook.