gpt4 book ai didi

python - 如何仅绘制seaborn 热图的下三角?

转载 作者:行者123 更新时间:2023-12-01 07:26:35 30 4
gpt4 key购买 nike

我有一个列表列表all_genres,其中每个子列表包含特定电视节目的类型,每个不同的子列表指不同的电视节目。我想绘制一个 Seaborn 热图,但只有下部三角形部分。这是我到目前为止所尝试过的:

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

结果图是这样的: seaborn plot

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

result

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

最佳答案

聚会有点晚了!...另一种方式

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)

关于python - 如何仅绘制seaborn 热图的下三角?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57414771/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com