colormap颜色映射

[python] view plain copy
import numpy as np  
import matplotlib.pyplot as plt  

# Have colormaps separated into categories:  
# http://matplotlib.org/examples/color/colormaps_reference.html  

cmaps = [('Perceptually Uniform Sequential',  
                            ['viridis', 'inferno', 'plasma', 'magma']),  
         ('Sequential',     ['Blues', 'BuGn', 'BuPu',  
                             'GnBu', 'Greens', 'Greys', 'Oranges', 'OrRd',  
                             'PuBu', 'PuBuGn', 'PuRd', 'Purples', 'RdPu',  
                             'Reds', 'YlGn', 'YlGnBu', 'YlOrBr', 'YlOrRd']),  
         ('Sequential (2)', ['afmhot', 'autumn', 'bone', 'cool',  
                             'copper', 'gist_heat', 'gray', 'hot',  
                             'pink', 'spring', 'summer', 'winter']),  
         ('Diverging',      ['BrBG', 'bwr', 'coolwarm', 'PiYG', 'PRGn', 'PuOr',  
                             'RdBu', 'RdGy', 'RdYlBu', 'RdYlGn', 'Spectral',  
                             'seismic']),  
         ('Qualitative',    ['Accent', 'Dark2', 'Paired', 'Pastel1',  
                             'Pastel2', 'Set1', 'Set2', 'Set3']),  
         ('Miscellaneous',  ['gist_earth', 'terrain', 'ocean', 'gist_stern',  
                             'brg', 'CMRmap', 'cubehelix',  
                             'gnuplot', 'gnuplot2', 'gist_ncar',  
                             'nipy_spectral', 'jet', 'rainbow',  
                             'gist_rainbow', 'hsv', 'flag', 'prism'])]  


nrows = max(len(cmap_list) for cmap_category, cmap_list in cmaps)  
gradient = np.linspace(0, 1, 256)  
gradient = np.vstack((gradient, gradient))  


def plot_color_gradients(cmap_category, cmap_list):  
    fig, axes = plt.subplots(nrows=nrows)  
    fig.subplots_adjust(top=0.95, bottom=0.01, left=0.2, right=0.99)  
    axes[0].set_title(cmap_category + ' colormaps', fontsize=14)  

    for ax, name in zip(axes, cmap_list):  
        ax.imshow(gradient, aspect='auto', cmap=plt.get_cmap(name))  
        pos = list(ax.get_position().bounds)  
        x_text = pos[0] - 0.01  
        y_text = pos[1] + pos[3]/2.  
        fig.text(x_text, y_text, name, va='center', ha='right', fontsize=10)  

    # Turn off *all* ticks & spines, not just the ones with colormaps.  
    for ax in axes:  
        ax.set_axis_off()  

for cmap_category, cmap_list in cmaps:  
    plot_color_gradients(cmap_category, cmap_list)  

plt.show() 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
举例:
横坐标是从1到1000的数,纵坐标是横坐标的平方,要求使用颜色映射为:gist_rainbow(即最后一张图的倒数第四个颜色映射)

import matplotlib.pyplot as plt

x = list(range(1,1001))
y = [i**2 for i in x]#使用列表解析

plt.scatter(x,y,c=y,cmap=plt.cm.gist_rainbow,s=20)#cm即colormap,c=y表示颜色随y变化
plt.xlabel('number',fontsize=10)
plt.ylabel('number square',fontsize=10)
plt.title('figure 1',fontsize =20)
plt.axis([0,1100,0,1100000])
plt.show()

 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

代码中cm即colormap,c=y表示颜色随y变化(第一图),也可以使用c=x,使得颜色随x映射(第二图),变化不大
图如下:
这里写图片描述
这里写图片描述

</article>
### Colormap 的逆映射实现 Colormap 的逆映射是一种将颜色值转换回原始数据范围的技术。这种技术通常用于可视化领域中的数据分析,尤其是在处理图像或体积渲染时。以下是关于 colormap 逆映射的一些关键概念和技术细节: #### 基本原理 Colormap 是一种将数值范围映射到颜色空间的方法。对于给定的颜色 \( C \),其对应的原始数据值 \( D \) 可通过反向查找来获得。这一过程依赖于已知的 colormap 定义及其特性。 如果 colormap 被定义为线性的,则可以通过简单的数学运算完成逆映射: \[ D = f^{-1}(C), \text{其中 } f(D) \text{ 表示正向映射函数} [^1].\] 然而,在实际应用中,colormap 往往是非线性的或者由离散点插值得到。因此,逆映射可能需要更复杂的计算方法。 #### 实现方式 以下是一个基于 Python 和 NumPy 的简单实现方案,展示如何执行 colormap 的逆映射操作: ```python import numpy as np from matplotlib import cm def colormap_inverse_mapping(color, cmap_name='viridis'): """ 将颜色值逆映射回原始数据范围。 参数: color (tuple): 输入颜色值 (R,G,B,A 或 R,G,B). cmap_name (str): 使用的 colormap 名称. 返回: float: 对应的数据值范围内的位置。 """ # 加载指定的 colormap cmap = cm.get_cmap(cmap_name) # 创建一个均匀分布的样本数组 n_samples = 10000 data_range = np.linspace(0, 1, n_samples).reshape(-1, 1) # 获取 colormap 中对应的颜色矩阵 colors_in_map = cmap(data_range) # 计算输入颜色与 colormap 颜色之间的欧几里得距离 distances = np.linalg.norm(colors_in_map - color[:3], axis=1) # 找到最近的距离索引 closest_index = np.argmin(distances) # 映射回原始数据范围 mapped_value = data_range[closest_index][0] return mapped_value # 测试代码 color_to_test = (0.298, 0.447, 0.69) # 示例颜色 result = colormap_inverse_mapping(color_to_test, 'viridis') print(f"Inverse Mapped Value: {result}") ``` 上述代码片段展示了如何利用 Matplotlib 提供的 colormap 进行逆映射。它通过比较目标颜色与 colormap 上所有可能的颜色来进行匹配,并返回最接近的目标数据值。 #### 注意事项 - 如果 colormap 不连续或存在重复区域,则可能导致多解情况。 - 数字精度会影响最终的结果准确性,特别是在高分辨率 colormaps 下。 - 当前实现假设输入颜色属于该 colormap;如果不是,则需额外验证逻辑。 #### 性能优化建议 为了提高效率,可以预先构建 colormap 到数据值的查找表(Lookup Table),从而减少实时计算的需求。这种方法特别适用于固定 colormap 场景下的批量处理任务。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值