使用Python Seaborn绘制热力图(heatmap)的时候怎么改变配色

文章介绍了如何使用Python的seaborn库创建Transformer模型注意力权重的heatmap,并通过参数cmap调整色彩空间。用户可以通过指定不同的预定义色彩映射如Greens_r来改变颜色显示。错误输入cmap参数时,系统会显示所有支持的颜色选项列表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

看到最近有些论文中会对Transformer encoder的attention weights进行可视化,通常会使用heatmap,我参考了一些博客,感觉已经总结得很详细了,例如这篇:python绘制热度图(heatmap)_黄思博呀的博客-优快云博客_python heatmap

不过我觉得有一点说得不是很清楚,我看完之后还是不知道怎么可以修改配色,参考一下官方文档:seaborn.heatmap — seaborn 0.12.2 documentation

只是说可以用cmap来定义color space:

cmap matplotlib colormap name or object, or list of colors, optional

The mapping from data values to color space. If not provided, the default will depend on whether center is set.

但是完全不知道怎么用啊,具体来说,可以加上cmap这个参数,例如:

plot=sns.heatmap(p_array, cmap='Greens_r')

具体的色彩空间可以参考这里:Choosing Colormaps in Matplotlib — Matplotlib 3.6.3 documentation

其实如果我们故意输错这个参数的话,报错信息里会提示所有可能的参数:

supported values are 'Accent', 'Accent_r', 'Blues', 'Blues_r', 'BrBG', 'BrBG_r', 'BuGn', 'BuGn_r', 'BuPu', 'BuPu_r', 'CMRmap', 'CMRmap_r', 'Dark2', 'Dark2_r', 'GnBu', 'GnBu_r', 'Greens', 'Greens_r', 'Greys', 'Greys_r', 'OrRd', 'OrRd_r', 'Oranges', 'Oranges_r', 'PRGn', 'PRGn_r', 'Paired', 'Paired_r', 'Pastel1', 'Pastel1_r', 'Pastel2', 'Pastel2_r', 'PiYG', 'PiYG_r', 'PuBu', 'PuBuGn', 'PuBuGn_r', 'PuBu_r', 'PuOr', 'PuOr_r', 'PuRd', 'PuRd_r', 'Purples', 'Purples_r', 'RdBu', 'RdBu_r', 'RdGy', 'RdGy_r', 'RdPu', 'RdPu_r', 'RdYlBu', 'RdYlBu_r', 'RdYlGn', 'RdYlGn_r', 'Reds', 'Reds_r', 'Set1', 'Set1_r', 'Set2', 'Set2_r', 'Set3', 'Set3_r', 'Spectral', 'Spectral_r', 'Wistia', 'Wistia_r', 'YlGn', 'YlGnBu', 'YlGnBu_r', 'YlGn_r', 'YlOrBr', 'YlOrBr_r', 'YlOrRd', 'YlOrRd_r', 'afmhot', 'afmhot_r', 'autumn', 'autumn_r', 'binary', 'binary_r', 'bone', 'bone_r', 'brg', 'brg_r', 'bwr', 'bwr_r', 'cividis', 'cividis_r', 'cool', 'cool_r', 'coolwarm', 'coolwarm_r', 'copper', 'copper_r', 'crest', 'crest_r', 'cubehelix', 'cubehelix_r', 'flag', 'flag_r', 'flare', 'flare_r', 'gist_earth', 'gist_earth_r', 'gist_gray', 'gist_gray_r', 'gist_heat', 'gist_heat_r', 'gist_ncar', 'gist_ncar_r', 'gist_rainbow', 'gist_rainbow_r', 'gist_stern', 'gist_stern_r', 'gist_yarg', 'gist_yarg_r', 'gnuplot', 'gnuplot2', 'gnuplot2_r', 'gnuplot_r', 'gray', 'gray_r', 'hot', 'hot_r', 'hsv', 'hsv_r', 'icefire', 'icefire_r', 'inferno', 'inferno_r', 'jet', 'jet_r', 'magma', 'magma_r', 'mako', 'mako_r', 'nipy_spectral', 'nipy_spectral_r', 'ocean', 'ocean_r', 'pink', 'pink_r', 'plasma', 'plasma_r', 'prism', 'prism_r', 'rainbow', 'rainbow_r', 'rocket', 'rocket_r', 'seismic', 'seismic_r', 'spring', 'spring_r', 'summer', 'summer_r', 'tab10', 'tab10_r', 'tab20', 'tab20_r', 'tab20b', 'tab20b_r', 'tab20c', 'tab20c_r', 'terrain', 'terrain_r', 'turbo', 'turbo_r', 'twilight', 'twilight_r', 'twilight_shifted', 'twilight_shifted_r', 'viridis', 'viridis_r', 'vlag', 'vlag_r', 'winter', 'winter_r'

最后附上一张我做的图:

 

### 如何设置 Seaborn 力图颜色方案 Seaborn 提供了多种方式来自定义力图颜色方案。通过 `cmap` 参数可以轻松更改颜色映射方案,从而实现不同风格的视觉效果。 #### 使用预设的颜色映射方案 Seaborn 和 Matplotlib 都内置了许多常用的颜色映射表单,可以直接作为字符串传递给 `cmap` 参数: ```python import numpy as np import seaborn as sns import matplotlib.pyplot as plt # 创建随机数据 data = np.random.rand(10, 10) # 绘制力图并应用预设颜色映射方案 "coolwarm" sns.heatmap(data, cmap="coolwarm", annot=True) plt.show() ``` 此代码片段展示了如何使用 `"coolwarm"` 这样的预设颜色映射方案[^2]。 #### 应用自定义离散色彩条带 如果希望创建具有特定分隔区间的离散色彩条带,则可以通过调用 `ListedColormap` 来构建自己的颜色列表: ```python from matplotlib.colors import ListedColormap custom_cmap = ListedColormap(["green", "yellow", "red"]) sns.heatmap(data, cmap=custom_cmap, annot=True) plt.show() ``` 这段代码说明了怎样通过组合不同的 RGB 值来定制独特的配色方案。 #### 利用渐变色板生成连续变化的颜色序列 对于需要平滑过渡的效果,可以选择基于现有色调调整亮度或饱和度的方式生成新的渐变色板: ```python colors = ["lightblue", "darkblue"] nodes = [0.25, 0.75] my_palette = sns.blend_palette(colors, n_colors=9, input="rgb") sns.heatmap(data, cmap=my_palette, center=0., annot=True) plt.show() ``` 上述例子解释了如何混合两种基本颜色,并按照一定比例分配权重以形成中间状态的颜色集合[^4]。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值