05.b sns.clustermap with network.csv

Plot a matrix dataset as a hierarchically-clustered heatmap.

Note

The hierarchical clustering has been deactivated.

plot main05 b clustermap
11 import pandas as pd
12 import seaborn as sns
13 import matplotlib.pyplot as plt
14
15 # Load dataset
16 networks = sns.load_dataset("brain_networks",
17     index_col=0, header=[0, 1, 2])
18
19 # Create variables
20 network_labels = networks.columns.get_level_values("network")
21 network_pal = sns.cubehelix_palette(
22     network_labels.unique().size,
23     light=.9, dark=.1, reverse=True,
24     start=1, rot=-2)
25 network_lut = dict(zip(map(str, network_labels.unique()), network_pal))
26 network_colors = pd.Series(network_labels).map(network_lut)
27
28 # The side colors are drawn with a heatmap, which matplotlib thinks
29 # of as quantitative data and thus there's not a straightforward way
30 # to get a legend directly from it. Instead of that, we'll add an
31 # invisible barplot with the right colors and labels, then add a
32 # legend for that.
33 g = sns.clustermap(networks.corr(),
34     row_cluster=False, col_cluster=False,                 # turn-off clustering
35     row_colors=network_colors, col_colors=network_colors, # add class labels
36     linewidths=0, xticklabels=False, yticklabels=False)   # improve visual wih many rows
37
38 for label in network_labels.unique():
39     g.ax_col_dendrogram.bar(0, 0,
40         color=network_lut[label],
41         label=label, linewidth=0)
42
43 # Move the colorbar to the empty space.
44 g.ax_col_dendrogram.legend(loc="center", ncol=6)
45 g.cax.set_position([.15, .2, .03, .45])
46
47 # Show
48 plt.show()

Total running time of the script: ( 0 minutes 0.442 seconds)

Gallery generated by Sphinx-Gallery