简体   繁体   English

如何将那些保持在 sns.heatmap 的对角线上?

[英]How to keep the ones in the Diagonal of sns.heatmap?

I want to plot the correlation Matrix with sns.heatmap and have some questions.我想 plot 与 sns.heatmap 的相关矩阵并有一些问题。 This is my code:这是我的代码:

plt.figure(figsize=(8,8)) mask =np.zeros_like(data.corr()) mask[np.triu_indices_from(mask)] = True sns.heatmap(data.corr(), mask=mask, linewidth=1, annot=True, fmt=".2f",cmap='coolwarm',vmin=-1, vmax=1) plt.show()

and this is what i get: [Correlation Matrix][1] [1]: https://i.stack.imgur.com/DX2oN.png \这就是我得到的:[相关矩阵][1] [1]: https://i.stack.imgur.com/DX2oN.png \

Now i have some questions:现在我有一些问题:

1) How can i keep the ones in the diagonale? 1)我怎样才能把那些放在对角线上?

2) How can i change the position of the x-axis? 2)如何改变x轴的position?

3) I want that the colorbar goes from 1 till -1, but the code is not working 3) 我希望颜色条从 1 变为 -1,但代码不工作

I hope someone can help.我希望有人能帮帮忙。

Thx谢谢

I think you have to check data.corr() , because your code is correct and gives the diagnoal (see below). 我认为您必须检查data.corr() ,因为您的代码正确并且可以诊断(请参阅下文)。 One question is: you use np.triu but the picture you show displays np.tirl . 一个问题是:您使用np.triu但显示的图片显示np.tirl

Here the code I've tested - the diagonal is there: 这是我测试过的代码-对角线在这里:

N = 5
A = np.arange(N*N).reshape(N,N)

B = np.tril(A)

mask =np.zeros_like(A)
mask[np.triu_indices_from(mask)] = True

print('A'); print(A); print()
print('tril(A)'); print(B); print()
print('mask'); print(mask); print()

gives

A
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]
 [20 21 22 23 24]]

tril(A)
[[ 0  0  0  0  0]
 [ 5  6  0  0  0]
 [10 11 12  0  0]
 [15 16 17 18  0]
 [20 21 22 23 24]]

mask
[[1 1 1 1 1]
 [0 1 1 1 1]
 [0 0 1 1 1]
 [0 0 0 1 1]
 [0 0 0 0 1]]

edit: suplement 编辑:补充

you could re-fine the mask, eg 您可以重新调整面膜,例如

C = A *mask
D = np.where(C > 1, 1,C)
print('D'); print(D)

gives

D
[[0 1 1 1 1]
 [0 1 1 1 1]
 [0 0 1 1 1]
 [0 0 0 1 1]
 [0 0 0 0 1]]

The first element of the diagonal of D is now a Zero since the first element of the diagonal of A is a Zero too. D的对角线的第一个元素现在为零,因为A的对角线的第一个元素也为零。

edit: suplement 2 编辑:补充2

F = np.tril(A,-1)
E = np.eye(N)
G = E + F

print('F'); print(F); print()
print('E'); print(E); print()
print('G'); print(G); print()

gives

F
[[ 0  0  0  0  0]
 [ 5  0  0  0  0]
 [10 11  0  0  0]
 [15 16 17  0  0]
 [20 21 22 23  0]]

E
[[1. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0.]
 [0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 1.]]

G
[[ 1.  0.  0.  0.  0.]
 [ 5.  1.  0.  0.  0.]
 [10. 11.  1.  0.  0.]
 [15. 16. 17.  1.  0.]
 [20. 21. 22. 23.  1.]]

mask[np.triu_indices_from(mask)] will define the triangle (including diagonal) mask[np.triu_indices_from(mask)]将定义三角形(包括对角线)

mask[np.eye(mask.shape[0], dtype=bool)] will define the diagonal. mask[np.eye(mask.shape[0], dtype=bool)]将定义对角线。

If you put those together, you can control them independently.如果将它们放在一起,则可以独立控制它们。 (Be aware you need to set the triangle before the diagonal). (请注意,您需要在对角线之前设置三角形)。

def plot_correlation_matrix(df, remove_diagonal=True, remove_triangle=False, **kwargs):
    corr = df.corr()
    # Apply mask
    mask = np.zeros_like(corr, dtype=np.bool)
    mask[np.triu_indices_from(mask)] = remove_triangle
    mask[np.eye(mask.shape[0], dtype=bool)] = remove_diagonal
    # Plot
    # plt.figure(figsize=(8,8))
    sns.heatmap(corr, mask=mask, **kwargs)
    plt.show()

So this command will generate the matrix, removing the upper triangle, but keeping the diagonal:所以此命令将生成矩阵,移除上三角,但保留对角线:

plot_correlation_matrix(df[colunas_notas], remove_diagonal=False, remove_triangle=True)

Change of the position of the x-axis x轴位置的变化

Since I'm not experienced with seaborn I would use matplotlib to plot the heat map ( here an example ) an then use matplotlib's twinx() or twiny() to place the axis where you want to have it ( here an example ). 由于我不seaborn经历我会使用matplotlib绘制热图( 这里的例子 )的再使用matplotlib的twinx()twiny()放置轴,你想拥有它( 这里的例子 )。

(I think that can be done with seaborn too - I just do not know it) (我认为也可以使用seaborn来完成-我只是不知道)

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

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