简体   繁体   English

Seaborn 热图中按行进行颜色缩放

[英]Color scale by rows in Seaborn Heatmap

I would like to make heatmap in Seaborn where color is scaled by rows.我想在 Seaborn 中制作热图,其中颜色按行缩放。 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?我怎么能做到?

This is my code:这是我的代码:

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这是我从中得到的热图

在此处输入图片说明

Using numpy.argsort you can find the order of values in each row.使用numpy.argsort您可以找到每行中值的顺序。 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()

在此处输入图片说明

Using pandas to divide each row by its maximum, we get a coloring where the maximum is dark red and the other columns depending on their relation to the maximum.使用 pandas 将每行除以其最大值,我们得到一个着色,其中最大值为深红色,其他列取决于它们与最大值的关系。 So a column almost equal to the maximum will be a lighter ted.因此,几乎等于最大值的列将是较轻的 ted。 A column with only half of the sales will be colored white.只有一半销售额的列将显示为白色。 A column with almost no sales will be blue.几乎没有销售额的列将是蓝色的。

The colorbar indicates the percentage compared to the maximum for each row.颜色条表示与每行最大值相比的百分比。

import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt
import random

sources = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
categories = [f'Cat {i}' for i in range(1, 5)]
data = [[s, c, random.randint(2, 50)] for s in sources for c in categories]
sales = pd.DataFrame(data, columns=['Sources', 'Category', 'Value'])

# create a dataframe grouped by Sources and Category
per_source_cat = sales.groupby(['Sources', 'Category']).agg({'Value': 'sum'})
# calculate the maximum for each source
max_per_source = sales.groupby(['Sources']).agg({'Value': 'max'})
# divide the sales of each source by the maximum for that source
per_source_cat = per_source_cat.div(max_per_source, level='Sources') * 100
# convert to a pivot table
per_source_cat = per_source_cat.pivot_table(index='Sources', columns='Category', values='Value')
# convert the sales to a compatible pivot table
sales = sales.pivot_table(index='Sources', columns='Category', values='Value')
sns.heatmap(per_source_cat, cmap='coolwarm', annot=sales, fmt='g', linewidths=1, linecolor='black', ).set_title('Sales')
plt.show()

示例热图

Alternatively, suppose you want to color the highest red and the lowest blue, independent whether they are close together or not.或者,假设您想为最高的红色和最低的蓝色着色,而不管它们是否靠近。 Then, subtracting the minimum and dividing by the difference between maximum and minimum could define the coloring.然后,减去最小值并除以最大值和最小值之间的差值可以定义着色。 A complete equal row causes a division by zero, which can be handled using fillna .完全相等的行会导致除以零,这可以使用fillna进行处理。

# create a dataframe grouped by Sources and Category
per_source_cat = sales.groupby(['Sources', 'Category']).agg({'Value': 'sum'})
# calculate the maximum and minimum for each source
max_per_source = sales.groupby(['Sources']).agg({'Value': 'max'})
min_per_source = sales.groupby(['Sources']).agg({'Value': 'min'})
# subtract the minimum and divide by the difference between maximum and minimum
per_source_cat = (per_source_cat - min_per_source) / (max_per_source - min_per_source) * 100
# in the case of all equal, there will be a division by 0, set every value to 100 %
per_source_cat = per_source_cat.fillna(100.0)

绘制最低颜色的蓝色

Now the colorbar indicates 100% for the highest, 0% for the lowest and the others colored in proportion.现在,颜色条指示最高为 100%,最低为 0%,其他按比例着色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM