英文:
heatmap excluding first non-NaN record in each row
问题
我正在使用seaborn构建热力图。我需要热力图从每行的第二个值开始。而且这个第二个值不是固定的列。如何实现这一点。这是我正在使用的代码:
fig, ax = plt.subplots(figsize=(15, 15))
sns.set(font_scale=0.7)
map1 = sns.heatmap(ax=ax, data=clv, annot=True, fmt='0.2f', cmap='Blues', linecolor='Grey', linewidth=.5, cbar=False)
st.write(fig)
我需要在每一行上显示热力图,但不包括每行的第一个值,这个值不是固定的。
这是示例数据:
Column A B C D E F G H I J K L M N
A 2.19 1.12 1.06 1.24 0.89 0.93 0.71 0.97 0.61 0.68 0.72 0.55 0.59 0.58
B 31.05 9.31 16.48 10.77 11.60 8.43 12.33 7.89 8.49 8.61 6.95 7.23 6.97
C 50.34 17.34 19.69 15.70 13.50 16.95 12.51 12.12 12.12 9.83 10.48 9.56
D 124.00 25.07 40.78 32.21 44.23 29.97 30.92 28.58 23.77 24.71 22.80
E 182.82 28.25 51.04 62.91 47.92 43.83 41.84 33.66 35.66 31.94
F 207.76 26.22 68.38 50.90 51.71 43.71 37.00 37.97 34.58
G 216.92 32.33 53.02 47.19 42.77 32.44 34.16 30.33
H 250.60 27.36 47.94 46.47 37.61 35.55 33.62
I 223.64 19.83 35.02 31.58 33.43 28.96
J 213.82 17.22 28.91 34.00 31.92
K 201.36 13.90 28.63 29.06
L 163.07 13.71 22.24
M 177.52 11.58
N 1701.76
这是数据的外观:
这是目前的外观。热力图是对角线生成的,而不是在每行中生成的。
英文:
I am building heat map using seaborn. I need heatmap to start from the second value in each row. And this second value is not a fixed column. How to achieve that. This is the code I am using:
fig, ax = plt.subplots(figsize=(15, 15))
sns.set(font_scale=0.7)
map1=sns.heatmap(ax = ax, data = clv, annot = True, fmt='0.2f', cmap = 'Blues',linecolor='Grey',linewidth=.5, cbar=False)
st.write(fig)
I need heatmap on each row, but excluding the first value in each row which is not fixed.
This is sample data:
Column A B C D E F G H I J K L M N
A 2.19 1.12 1.06 1.24 0.89 0.93 0.71 0.97 0.61 0.68 0.72 0.55 0.59 0.58
B 31.05 9.31 16.48 10.77 11.60 8.43 12.33 7.89 8.49 8.61 6.95 7.23 6.97
C 50.34 17.34 19.69 15.70 13.50 16.95 12.51 12.12 12.12 9.83 10.48 9.56
D 124.00 25.07 40.78 32.21 44.23 29.97 30.92 28.58 23.77 24.71 22.80
E 182.82 28.25 51.04 62.91 47.92 43.83 41.84 33.66 35.66 31.94
F 207.76 26.22 68.38 50.90 51.71 43.71 37.00 37.97 34.58
G 216.92 32.33 53.02 47.19 42.77 32.44 34.16 30.33
H 250.60 27.36 47.94 46.47 37.61 35.55 33.62
I 223.64 19.83 35.02 31.58 33.43 28.96
J 213.82 17.22 28.91 34.00 31.92
K 201.36 13.90 28.63 29.06
L 163.07 13.71 22.24
M 177.52 11.58
N 1701.76
This is how it's looking right now. Heatmap is generated diagonally not in each row.
答案1
得分: 2
我理解您想要对数据进行着色,而不考虑对角线部分。您可以为对角线创建一个掩码,然后在其上创建两个热力图。首先是一个只包含对角线的热力图,使用固定颜色(或者只是一个不同于主要热力图的颜色映射)。然后是其余部分的热力图。Seaborn将使掩码部分保持透明。
from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
df = pd.DataFrame([[2.19, 1.12, 1.06, 1.24, 0.89, 0.93, 0.71, 0.97, 0.61, 0.68, 0.72, 0.55, 0.59, 0.58],
[np.nan, 31.05, 9.31, 16.48, 10.77, 11.6, 8.43, 12.33, 7.89, 8.49, 8.61, 6.95, 7.23, 6.97],
[np.nan, np.nan, 50.34, 17.34, 19.69, 15.7, 13.5, 16.95, 12.51, 12.12, 12.12, 9.83, 10.48, 9.56],
[np.nan, np.nan, np.nan, 124.0, 25.07, 40.78, 32.21, 44.23, 29.97, 30.92, 28.58, 23.77, 24.71, 22.8],
[np.nan, np.nan, np.nan, np.nan, 182.82, 28.25, 51.04, 62.91, 47.92, 43.83, 41.84, 33.66, 35.66, 31.94],
[np.nan, np.nan, np.nan, np.nan, np.nan, 207.76, 26.22, 68.38, 50.9, 51.71, 43.71, 37.0, 37.97, 34.58],
[np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, 216.92, 32.33, 53.02, 47.19, 42.77, 32.44, 34.16, 30.33],
[np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, 250.6, 27.36, 47.94, 46.47, 37.61, 35.55, 33.62],
[np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, 223.64, 19.83, 35.02, 31.58, 33.43, 28.96],
[np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, 213.82, 17.22, 28.91, 34.0, 31.92],
[np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, 201.36, 13.9, 28.63, 29.06],
[np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, 163.07, 13.71, 22.24],
[np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, 177.52, 11.58],
[np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, 1701.76]],
columns=['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N'],
index=['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N'])
sns.set() # 设置默认的 Seaborn 样式
fig, ax = plt.subplots(figsize=(20, 12))
diagonal_mask = np.eye(len(df), dtype=bool) # 创建一个对角线为True的方形掩码矩阵
cmap_for_diag = sns.color_palette(['aliceblue'], as_cmap=True) # 创建一个单色的对角线颜色映射
common_keywords = dict(annot=True, fmt='.2f', linecolor='Grey', linewidth=0.5, ax=ax)
# 创建对角线部分的热力图
sns.heatmap(df, cmap=cmap_for_diag, mask=~diagonal_mask, cbar=False, **common_keywords)
# 创建其余部分的热力图
sns.heatmap(df, cmap='Spectral', mask=diagonal_mask, **common_keywords)
plt.tight_layout()
plt.show()
<details>
<summary>英文:</summary>
If I understand correctly, you want to color your data without taking into account the diagonal. You can create a mask for the diagonal, and create two heatmaps on top of each other. First a heatmap with only the diagonal, with a fixed color (or just a different colormap as the main heatmap). Then a heatmap with the rest. Seaborn will leave the masked away parts transparent.
```python
from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
df = pd.DataFrame([[2.19, 1.12, 1.06, 1.24, 0.89, 0.93, 0.71, 0.97, 0.61, 0.68, 0.72, 0.55, 0.59, 0.58],
[np.nan, 31.05, 9.31, 16.48, 10.77, 11.6, 8.43, 12.33, 7.89, 8.49, 8.61, 6.95, 7.23, 6.97],
[np.nan, np.nan, 50.34, 17.34, 19.69, 15.7, 13.5, 16.95, 12.51, 12.12, 12.12, 9.83, 10.48, 9.56],
[np.nan, np.nan, np.nan, 124.0, 25.07, 40.78, 32.21, 44.23, 29.97, 30.92, 28.58, 23.77, 24.71, 22.8],
[np.nan, np.nan, np.nan, np.nan, 182.82, 28.25, 51.04, 62.91, 47.92, 43.83, 41.84, 33.66, 35.66, 31.94],
[np.nan, np.nan, np.nan, np.nan, np.nan, 207.76, 26.22, 68.38, 50.9, 51.71, 43.71, 37.0, 37.97, 34.58],
[np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, 216.92, 32.33, 53.02, 47.19, 42.77, 32.44, 34.16, 30.33],
[np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, 250.6, 27.36, 47.94, 46.47, 37.61, 35.55, 33.62],
[np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, 223.64, 19.83, 35.02, 31.58, 33.43, 28.96],
[np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, 213.82, 17.22, 28.91, 34.0, 31.92],
[np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, 201.36, 13.9, 28.63, 29.06],
[np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, 163.07, 13.71, 22.24],
[np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, 177.52, 11.58],
[np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, 1701.76]],
columns=['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N'],
index=['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N'])
sns.set() # set default seaborn style
fig, ax = plt.subplots(figsize=(20,12))
diagonal_mask = np.eye(len(df), dtype=bool) # the mask is a square matrix with True on the diagonal
cmap_for_diag = sns.color_palette(['aliceblue'], as_cmap=True) # create a one-colored colormap for the diagonal
common_keywords = dict(annot=True, fmt='.2f', linecolor='Grey', linewidth=.5, ax=ax)
# create a heatmap for the diagonal
sns.heatmap(df, cmap=cmap_for_diag, mask=~diagonal_mask, cbar=False, **common_keywords)
# create a heatmap for the rest of the data
sns.heatmap(df, cmap='Spectral', mask=diagonal_mask, **common_keywords)
plt.tight_layout()
plt.show()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论