Python 使用df高亮显示热图中每列的平均值数据单元格的自定义颜色

Python 使用df高亮显示热图中每列的平均值数据单元格的自定义颜色,python,pandas,matplotlib,seaborn,Python,Pandas,Matplotlib,Seaborn,我试图突出显示熊猫热图中平均值的平均值单元格或最低值单元格,但它总是给我失败的结果。我想突出显示热图中精确平均值的单元格,如果精确值不可用,意味着需要突出显示平均值的最低值 对于Ex:Meanvalue为17.522,但在df中不可用意味着需要突出显示15.499(参考 在这里,我分享了我尝试的截图,以及我对你们裁判的期望 天才永远欢迎..!提前谢谢 每列平均值为 array([17.60950419, 33.73034387, 46.63401871, 56.27580645, 52.6295

我试图突出显示熊猫热图中平均值的平均值单元格或最低值单元格,但它总是给我失败的结果。我想突出显示热图中精确平均值的单元格,如果精确值不可用,意味着需要突出显示平均值的最低值

对于Ex:Meanvalue为17.522,但在df中不可用意味着需要突出显示15.499(参考

在这里,我分享了我尝试的截图,以及我对你们裁判的期望

天才永远欢迎..!提前谢谢

每列平均值为

array([17.60950419, 33.73034387, 46.63401871, 56.27580645, 52.62956452,
       63.70669355, 71.75735484, 67.788     , 83.62327419, 75.41342   ])
我尝试了以下代码来突出显示单个单元格

df_Mean=np.array(df.mean())

fig, ax = plt.subplots(figsize=(18,8))

cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", ["#f9f9f9","#B6DBF2","#327AD9","#3068D9"])
color = ["#f9f9f9",'#3068D9',"#f9f9f9","#f9f9f9","#B6DBF2","#327AD9","#3068D9"]

ax = sns.heatmap(df, annot=True, fmt=".5g", linewidths=.02, 
                 cmap=cmap, vmin=0, vmax=300,cbar_kws={'label': 'Si.No'}, 
                linecolor='#CBDBD7',
                ax = ax,
                xticklabels=1, yticklabels=1,
                )

ax = sns.heatmap(df.round(),mask=(df > df_Mean),
             vmin=10, vmax=80, cmap=color, cbar=False)

ax.invert_yaxis()
ax.yaxis.set_label_position("right")
ax.yaxis.tick_right()

ax.set_xticklabels(
    ax.get_xticklabels(), color = 'darkgreen',
    horizontalalignment='center');

ax.set_yticklabels(
    ax.get_yticklabels(), color = 'darkblue',
    horizontalalignment='right',
    size = 10,);

ax.xaxis.set_tick_params(pad=10)
ax.yaxis.set_tick_params(pad=20)  

plt.xlabel('Month', color = 'Maroon', size= 15)
plt.title('Testing_HeatMap', color = 'darkgreen', size = 20)
plt.show()
我得到了这个输出,

预期产出为:

在这里,我演示了一些随机数据的解决方案(但它应该说明您设置的方法)

对于每列,我找到最接近列平均值的值的行号,该行号小于或等于列平均值:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import pandas as pd
import seaborn as sns

# Generate some random data (25 rows, 3 columns)
df = pd.DataFrame(np.random.rand(25, 3))

# Compute the mean of each column
df_mean = df.mean()

# Find the difference between each value and the column mean
diff = df - df.mean()

# We are only interested in values less than or equal to the mean
mask = diff <= 0

# The row numbers of the closest values to the column mean
# which are less than or equal to the column mean
highlight_row = np.nanargmin(np.array(df[mask]), axis=0)
# Plotting
fig, ax = plt.subplots()
ax = sns.heatmap(df, ax=ax)

# Loop over the columns
for col in range(df.shape[1]):
    # Add a rectangle which highlights our cell of interest
    ax.add_patch(Rectangle((col, highlight_row[col]), 1, 1))