Created
June 9, 2021 00:08
-
-
Save afrendeiro/aeeb09209430fd7bd3500f51ff0420f3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import string | |
import numpy as np | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
import seaborn as sns | |
# generate some data | |
n_groups = 4 | |
n_per_group = 5 | |
n = n_groups * n_per_group | |
labels = pd.Categorical([x for x in string.ascii_uppercase[:n_groups] for y in range(n_per_group)], ordered=True) | |
colors = plt.get_cmap('tab10')(labels.codes) | |
x = pd.DataFrame(pd.get_dummies(labels).values + np.random.random((n, n_groups)), index=labels) | |
c = x.T.corr() | |
kws = dict(row_colors=colors, dendrogram_ratio=0.08, cbar_kws=dict(label='correlation'), figsize=(6.1, 6)) | |
# won't work because 'c' is not guaranteed to be ordered by dendrogram order | |
grid = sns.clustermap(c, mask=np.triu(c) == 0, **kws) | |
# Option 1: | |
# # first cluster and get order | |
grid1 = sns.clustermap(c) | |
order = grid1.dendrogram_row.reordered_ind | |
co = c.iloc[order, order] | |
plt.close(grid1.fig) | |
# # then, mask lowe halt | |
grid2 = sns.clustermap(co, mask=np.triu(co) == 0, xticklabels=False, row_cluster=False, **kws) | |
# # rotate yaxislabels | |
grid2.ax_heatmap.set_yticklabels(grid2.ax_heatmap.get_yticklabels(), rotation=0) | |
# Option 2: | |
# # plot | |
grid = sns.clustermap(c, xticklabels=False, **kws) | |
# # remove lower halt, by setting values to nan | |
mesh = grid.ax_heatmap.get_children()[0] | |
c[np.triu(c) == 0] = np.nan | |
mesh.set_array(c) | |
# # remove row dendrogram | |
grid.fig.delaxes(grid.ax_row_dendrogram) | |
# # rotate yaxislabels | |
grid.ax_heatmap.set_yticklabels(grid.ax_heatmap.get_yticklabels(), rotation=0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment