如何仅绘制seaborn热图的下三角形? [英] How to plot only the lower triangle of a seaborn heatmap?

查看:32
本文介绍了如何仅绘制seaborn热图的下三角形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表列表 all_genres ,其中每个子列表都包含特定电视节目的类型,每个不同的子列表都涉及不同的电视节目.我想绘制一个seaborn热图,但只有较低的三角形部分.这是我迄今为止尝试过的:

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()

生成的图片是这样的:

我只想得到这个热图的下部或底部,因为左下部分和右上部分是相同的,即它们只是对角线的反射.我想要得到的结果图片是这样的:

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 :

如何在seaborn或matplotlib中执行此操作?

How to do this in seaborn or in matplotlib?

推荐答案

晚点派对!...另一种方式

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)

这篇关于如何仅绘制seaborn热图的下三角形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆