Seaborn Heatmap中按行的颜色比例 [英] Color scale by rows in Seaborn Heatmap

查看:101
本文介绍了Seaborn Heatmap中按行的颜色比例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Seaborn中制作热图,其中颜色按行缩放。我的意思是,连续的最高值在图例上具有最高的颜色,而连续的最低值-最低。我该怎么办?

I would like to make heatmap in Seaborn where color is scaled by rows. I mean that the highest value in a row has the highest color on a legend and the lowest value in a row - the lowest. How could I do it?

这是我的代码:

sales = sales.pivot_table(index='Sources', columns='Category', values='Value')

sns.heatmap(sales,annot=True, cmap='coolwarm',fmt='g',linewidths=1,linecolor='black',).set_title('Sales')

这就是我从中获得的热点图

And this is heatmap that I am getting from this

推荐答案

使用 numpy.argsort 您可以在每一行中找到值的顺序。将结果用作着色的基础将为您提供每行的映射。

Using numpy.argsort you can find the order of values in each row. Using the result as base for colorization would give you the mapping per row.

import numpy as np; np.random.seed(42)
import matplotlib.pyplot as plt
from matplotlib.ticker import FixedFormatter

data = np.random.randint(1,250, size=(10,6))
b = np.argsort(np.argsort(data, axis=1), axis=1)

im = plt.imshow(b, aspect="auto", cmap="coolwarm")
plt.colorbar(im, ticks=np.array([0.0, 0.5, 1.0])*b.max(), 
             format=FixedFormatter(["low", "middle", "high"]))

for i in range(data.shape[0]):
    for j in range(data.shape[1]):
        plt.text(j,i,data[i,j], ha="center", va="center")

plt.show()

< img src = https://i.stack.imgur.com/42HwI.png alt =在此处输入图片描述>

这篇关于Seaborn Heatmap中按行的颜色比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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