我有一个列表all_genres的列表,其中每个子列表都包含特定电视节目的流派,每个不同的子列表都引用了不同的电视节目。我想绘制一个深海的热图,但只有较低的三角形部分。到目前为止,这是我尝试过的:

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


结果图片是这样的:
python - 如何仅绘制海洋热图的下部三角形?-LMLPHP

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

python - 如何仅绘制海洋热图的下部三角形?-LMLPHP

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

最佳答案

您可以查看https://seaborn.pydata.org/examples/many_pairwise_correlations.html并发现

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


将是用于掩盖矩阵上三角部分的代码,然后将sns.heatmap的代码更改为以下代码(如网站所建议):

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


之后,您可以自定义其他属性。

10-06 05:55