How to plot only the lower triangle of a seaborn heatmap?

眉间皱痕 提交于 2021-02-10 06:16:06

问题


I've a list of lists all_genres where each sub-lists contain genres for a particular tv-show and each different sub-list refers to different tv-shows. I want to plot a seaborn heatmap but with only the lower traiangle part. Here is what I've tried so far :

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(rc={'figure.figsize':(16,9)})

all_genres = [['Drama', 'History'], ['Documentary'], ['Action', 'Drama', 'History'], ['Documentary'], ['Crime', 'Drama', 'Thriller'], ['Action', 'Adventure', 'Drama'], ['Crime', 'Drama', 'Thriller'], ['Documentary'], ['Documentary'], ['Documentary'], ['Animation', 'Adventure', 'Comedy'], ['Crime', 'Drama'], ['Documentary', 'History', 'War'], ['Animation', 'Action', 'Adventure'], ['Documentary'], ['Documentary', 'Music'], ['Crime', 'Drama', 'Mystery'], ['Documentary', 'History', 'War'], ['Fantasy', 'Horror', 'Mystery'], ['Documentary'], ['Drama'], ['Animation', 'Action', 'Adventure'], ['Adventure', 'Drama', 'Sci-Fi'], ['Documentary', 'History', 'War'], ['Biography', 'Drama', 'History'], ['Crime', 'Drama', 'Mystery'], ['Crime', 'Drama', 'Thriller'], ['Animation', 'Action', 'Adventure'], ['Animation', 'Crime', 'Drama'], ['Comedy', 'News', 'Talk-Show'], ['Documentary'], ['Animation', 'Action', 'Adventure'], ['Drama', 'Sci-Fi', 'Thriller'], ['Documentary'], ['Animation', 'Action', 'Adventure'], ['Comedy'], ['Comedy', 'Romance'], ['Drama', 'Romance'], ['Documentary'], ['Comedy'], ['Drama', 'Thriller', 'War'], ['Comedy'], ['Documentary', 'History', 'War'], ['Drama', 'Fantasy', 'Horror'], ['Animation', 'Adventure', 'Comedy'], ['Crime', 'Drama', 'Fantasy'], ['Comedy'], ['Animation', 'Adventure', 'Drama'], ['Comedy', 'Drama'], ['Comedy', 'Drama'], ['Biography', 'Crime', 'Drama'], ['Comedy', 'Drama'], ['Biography', 'Drama', 'History'], ['Comedy'], ['Drama'], ['Crime', 'Drama'], ['Comedy', 'War'], ['Drama', 'Mystery', 'Sci-Fi'], ['Action', 'Comedy', 'Drama'], ['Action', 'Drama', 'History'], ['Animation', 'Action', 'Adventure'], ['Comedy'], ['Crime', 'Drama', 'Thriller'], ['Animation', 'Comedy'], ['Crime', 'Drama', 'Mystery'], ['Crime', 'Thriller'], ['Animation', 'Action', 'Adventure'], ['Comedy'], ['Animation', 'Comedy'], ['Drama'], ['Drama', 'Mystery'], ['Comedy', 'Music'], ['Documentary', 'Comedy'], ['Comedy', 'Drama'], ['Animation', 'Action', 'Adventure'], ['Crime', 'Drama', 'History'], ['Animation', 'Action', 'Adventure'], ['Crime', 'Drama', 'Mystery'], ['Animation', 'Comedy', 'Drama'], ['Adventure', 'Drama', 'Western'], ['Documentary', 'Talk-Show'], ['Documentary', 'Crime'], ['Action', 'Adventure', 'Drama'], ['Drama', 'Horror', 'Mystery'], ['Crime', 'Drama'], ['Comedy'], ['Comedy', 'Drama'], ['Crime', 'Drama', 'Mystery'], ['Drama', 'History'], ['Crime', 'Drama', 'Mystery'], ['Crime', 'Drama', 'Mystery'], ['Crime', 'Drama', 'Thriller'], ['Comedy', 'Talk-Show'], ['Drama', 'Romance'], ['Animation', 'Crime', 'Drama'], ['Crime', 'Drama', 'Mystery'], ['Crime', 'Drama', 'Mystery'], ['Comedy'], ['Animation', 'Action', 'Adventure'], ['Animation', 'Action', 'Comedy'], ['Action', 'Drama', 'History'], ['Crime', 'Mystery', 'Thriller'], ['Comedy', 'Talk-Show'], ['Action', 'Adventure', 'Mystery'], ['Animation', 'Short', 'Comedy'], ['Action', 'Crime', 'Drama'], ['Comedy'], ['Comedy', 'Drama', 'Romance'], ['Animation', 'Action', 'Adventure'], ['Documentary', 'Crime'], ['Comedy', 'Drama', 'Music'], ['Comedy'], ['Drama', 'Romance'], ['Action', 'Crime', 'Drama'], ['Animation', 'Action', 'Adventure'], ['Drama', 'History', 'War'], ['Crime', 'Drama', 'Thriller'], ['Drama'], ['Action', 'Comedy'], ['Animation', 'Comedy'], ['Documentary', 'Adventure'], ['Documentary'], ['Comedy', 'Drama'], ['Animation', 'Action', 'Adventure'], ['Comedy', 'Game-Show'], ['Drama', 'Sport'], ['Adventure', 'Drama', 'Family'], ['Comedy'], ['Comedy'], ['Crime', 'Drama', 'Mystery'], ['Comedy'], ['Adventure', 'Comedy', 'Drama'], ['Family', 'Fantasy', 'Music'], ['Comedy', 'Drama'], ['Drama', 'Romance'], ['Animation', 'Action', 'Adventure'], ['Drama'], ['Action', 'Crime', 'Drama'], ['Comedy', 'Music', 'Musical'], ['Comedy'], ['Animation', 'Action', 'Drama'], ['Documentary'], ['Crime', 'Drama', 'Mystery'], ['Drama', 'War'], ['Animation', 'Action', 'Adventure'], ['Crime', 'Drama', 'Mystery'], ['Comedy', 'Drama'], ['Documentary', 'Biography', 'Crime'], ['Crime', 'Drama', 'History'], ['Comedy'], ['Animation', 'Action', 'Adventure'], ['Animation', 'Action', 'Adventure'], ['Biography', 'Drama', 'History'], ['Comedy', 'Talk-Show'], ['Comedy', 'Sci-Fi'], ['Comedy'], ['Crime', 'Drama'], ['Comedy', 'Crime', 'Drama'], ['Comedy', 'Drama'], ['Comedy', 'Romance'], ['Animation', 'Action', 'Adventure'], ['Crime', 'Drama', 'Mystery'], ['Action', 'Adventure', 'Crime'], ['Animation', 'Action', 'Adventure'], ['Comedy'], ['Animation', 'Action', 'Adventure'], ['Crime', 'Drama', 'Thriller'], ['Comedy', 'Family'], ['Documentary', 'History', 'War'], ['Biography', 'Drama'], ['Comedy'], ['Comedy', 'Musical', 'Sci-Fi'], ['Animation', 'Comedy', 'Drama'], ['Documentary', 'Sport'], ['Comedy', 'Game-Show'], ['Action', 'Adventure', 'Drama'], ['Comedy', 'Reality-TV'], ['Animation', 'Action', 'Adventure'], ['Drama', 'Romance'], ['Crime', 'Drama', 'Horror'], ['Comedy'], ['Comedy', 'Drama', 'Family'], ['Crime', 'Drama', 'Thriller'], ['Crime', 'Drama', 'Mystery'], ['Animation', 'Drama', 'Fantasy'], ['Action', 'Adventure', 'Biography'], ['Comedy', 'Drama'], ['Drama', 'Fantasy', 'Romance'], ['Action', 'Crime', 'Mystery'], ['Crime', 'Drama', 'Thriller'], ['Crime', 'Drama', 'Mystery'], ['Crime', 'Drama', 'Thriller'], ['Drama', 'Mystery', 'Sci-Fi'], ['Drama', 'Sci-Fi', 'Thriller'], ['Animation', 'Comedy', 'Sci-Fi'], ['Crime', 'Drama', 'Mystery'], ['Drama', 'Thriller'], ['Comedy'], ['Drama'], ['Comedy'], ['Animation', 'Action', 'Crime'], ['Biography', 'Crime', 'Drama'], ['Biography', 'Drama', 'History'], ['Comedy'], ['Action', 'Adventure', 'Biography'], ['Action', 'Comedy', 'Fantasy'], ['Crime', 'Drama'], ['Crime', 'Drama'], ['Crime', 'Drama'], ['Comedy', 'Drama', 'Romance'], ['Animation', 'Action', 'Adventure'], ['Animation', 'Action', 'Family'], ['Drama'], ['Animation', 'Comedy', 'Sci-Fi'], ['Action', 'Drama', 'History'], ['Crime', 'Drama', 'Mystery'], ['Crime', 'Drama'], ['Comedy', 'News', 'Talk-Show'], ['Animation', 'Action', 'Adventure'], ['Animation', 'Action', 'Adventure'], ['Drama', 'Mystery', 'Thriller'], ['Comedy', 'News', 'Talk-Show'], ['Comedy', 'Fantasy', 'Horror'], ['Drama', 'Fantasy', 'Mystery'], ['Adventure', 'Fantasy'], ['Animation', 'Action', 'Adventure'], ['Comedy', 'Crime'], ['Crime', 'Drama'], ['Comedy', 'Drama'], ['Drama', 'Mystery', 'Sci-Fi'], ['Comedy', 'Drama'], ['Comedy', 'Talk-Show'], ['Drama'], ['Animation', 'Family', 'Comedy'], ['Comedy', 'Horror'], ['Comedy', 'Drama'], ['Drama'], ['Documentary', 'Short'], ['Crime', 'Drama', 'Mystery'], ['Comedy', 'Romance'], ['Comedy', 'Family', 'Music'], ['Drama', 'Mystery', 'Sci-Fi'], ['Comedy', 'Crime']]
unique_genres = sorted({i for j in all_genres for i in j})
genre_matrix = []
for i in unique_genres:
    temp = []
    for j in unique_genres:
        s = sum(1  if i in m and j in m and i!=j else 0 for m in all_genres)
        temp.append(s)
    genre_matrix.append(temp)
genre_matrix = np.array(genre_matrix)

ax = sns.heatmap(genre_matrix, xticklabels=unique_genres, yticklabels=unique_genres, annot=True, linewidths=.42, cbar=True, cbar_kws={'label': 'Colorbar'})
#plt.savefig('seaborn_cross_fold_plot.png', bbox_inches="tight", pad_inches=0)
plt.show()

The resultant picture is this :

I want to get only the lower or bottom part of this heatmap as the the left bottom part and upper right part would be same i.e. they are just reflection wrt the diagonal line. The resultant picture I want to get is something like this :

How to do this in seaborn or in matplotlib?


回答1:


You can look at https://seaborn.pydata.org/examples/many_pairwise_correlations.html and find out that

mask = np.zeros_like(corr, dtype=np.bool)
mask[np.triu_indices_from(mask)] = True

will be the code for masking the upper triangle part of the matrix.And then you would change the code of sns.heatmap to the following (as the website suggests):

sns.heatmap(corr, mask=mask, cmap=cmap, vmax=.3, center=0,
            square=True, linewidths=.5, cbar_kws={"shrink": .5})

You can customize other attributes if you want after that.




回答2:


Bit a late to the Party!... Another way

corr= df_new.corr()

# Getting the Upper Triangle of the co-relation matrix
matrix = np.triu(corr)

# using the upper triangle matrix as mask 
sns.heatmap(corr, annot=True, mask=matrix)


来源:https://stackoverflow.com/questions/57414771/how-to-plot-only-the-lower-triangle-of-a-seaborn-heatmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!