Seaborn HeatMap - 如何在多个不同的数据集中设置颜色分级 [英] Seaborn HeatMap - How to set colour grading throughout multiple different datasets

查看:5069
本文介绍了Seaborn HeatMap - 如何在多个不同的数据集中设置颜色分级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我需要用不同的数据库来创建海量的热图。一些范围从0-100和一些+100到-100。我需要做的是在所有图表中保持颜色分级相同。因此,例如,我希望0以下的任何东西都能稳定地从深蓝色变为浅蓝色,并且任何大于0的变化都会变得更暗,例如下面的可怕示例图。





我不需要下面的内容非常好的是一个流体的颜色过渡,因为目前我不完全确定seaborn是如何工作的,因为我刚刚列出了许多颜色 - 代码如下

  sns.heatmap(df.T,cmap = ListedColormap(['#000066','#000099','#0000cc','#1a1aff','#6666ff','#b3b3ff',' #ffff00','#ffcccc','#ff9999','#ff6666','#ff3333','#ff0000']),annot = False)

感谢您的任何建议。 为了指定颜色标准化,您可以使用 Normalize 实例, plt.Normalize(vmin,vmax),并使用 norm 关键字(路由到底层的 pcolormesh )。



要获得颜色逐渐变化的颜色映射,您可以使用静态 LinearSegmentedColormap.from_list 方法并为其提供颜色列表。

  import numpy as np; np.random.seed(0)
导入seaborn作为sns
导入matplotlib.pyplot作为plt
导入matplotlib.colors作为mcolors

x1 = np.random。 randint(0,100,size =(12,8))
x2 = np.random.randint(-100,100,size =(12,8))

fig,axes = plt.subplots (ncols = 2)

cmap = mcolors.LinearSegmentedColormap.from_list(n,['#000066','#000099','#0000cc','#1a1aff','#6666ff' ,'#b3b3ff',
'#ffff00','#ffcccc','#ff9999','#ff6666','#ff3333','#ff0000'])
norm = plt.Normalize (-100,100)

sns.heatmap(x1,ax = axes [0],cmap = cmap,norm = norm)
sns.heatmap(x2,ax = axes [1], cmap = cmap,norm = norm)

plt.show()


So I need to create a number of heatmaps in seaborn with varying datascales. Some range from 0-100 and some +100 to -100. What I need to do is to keep the colour grading the same throughout all graphs. So for example I want anything below 0 to be steadily getting from dark blue to light blue and anything above 0 to be getting darker red such as the terrible example graph below.

What I need that is not shown below very well is a fluid colour transition as currently I am not fully sure how seaborn is working it out as I have just listed a number of colours - Code below

sns.heatmap(df.T, cmap=ListedColormap(['#000066','#000099','#0000cc','#1a1aff','#6666ff','#b3b3ff','#ffff00','#ffcccc','#ff9999','#ff6666','#ff3333','#ff0000']), annot=False)

Thanks for any advise.

解决方案

To specify the color normalization, you can use a Normalize instance, plt.Normalize(vmin, vmax) and supply it to the heatmap using the norm keyword (which is routed to the underlying pcolormesh).

To obtain a colormap with gradually changing colors, you may use the static LinearSegmentedColormap.from_list method and supply it with a list of colors.

import numpy as np; np.random.seed(0)
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors

x1 = np.random.randint(0,100,size=(12,8))
x2 = np.random.randint(-100,100,size=(12,8))

fig, axes = plt.subplots(ncols=2)

cmap = mcolors.LinearSegmentedColormap.from_list("n",['#000066','#000099','#0000cc','#1a1aff','#6666ff','#b3b3ff',
                       '#ffff00','#ffcccc','#ff9999','#ff6666','#ff3333','#ff0000'])
norm = plt.Normalize(-100,100)

sns.heatmap(x1, ax=axes[0], cmap=cmap, norm=norm)
sns.heatmap(x2, ax=axes[1], cmap=cmap, norm=norm)

plt.show()

这篇关于Seaborn HeatMap - 如何在多个不同的数据集中设置颜色分级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆