Note
Click here to download the full example code
05.a sns.clustermap
basic sample
Plot a matrix dataset as a hierarchically-clustered heatmap.
Note
The hierarchical clustering has been deactivated.
Out:
A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 A15 A16 A17 A18 A19
A0 3 0 0 0 2 2 2 0 8 4 1 5 3 8 2 3 7 3 3 9
A1 2 4 6 4 2 1 4 8 2 7 3 7 3 4 8 9 1 8 1 9
A2 0 0 3 1 7 6 6 2 5 9 8 0 7 0 0 6 9 2 7 8
A3 4 5 4 2 1 9 0 9 7 1 1 3 2 0 7 2 1 1 1 9
A4 0 1 0 1 6 4 2 2 6 8 8 7 5 9 2 0 2 4 8 9
A5 5 3 8 5 4 2 4 7 3 3 2 9 9 1 0 5 6 3 0 2
A6 5 8 7 9 6 6 1 4 3 9 8 4 4 8 1 8 0 6 1 3
A7 2 8 2 8 8 1 5 9 9 7 0 6 3 9 2 0 4 4 6 3
A8 1 5 6 1 5 7 7 3 3 2 1 9 8 1 0 8 8 7 9 2
A9 1 9 8 4 0 8 2 4 0 4 2 2 2 3 3 5 2 8 1 0
A10 8 6 7 8 0 7 5 9 9 4 0 0 3 0 1 4 1 3 6 9
A11 3 7 8 6 4 4 8 7 0 3 1 8 8 0 0 2 5 9 8 9
A12 8 4 6 0 3 2 9 3 6 5 9 0 3 6 1 5 1 3 1 3
A13 5 8 8 4 6 5 8 4 4 8 8 2 5 2 2 2 3 5 1 6
A14 0 0 4 6 1 8 9 4 3 1 3 3 7 3 3 4 6 4 0 5
A15 8 2 4 7 8 0 7 8 3 4 0 9 8 7 3 4 5 1 2 2
A16 4 6 7 6 8 8 5 2 6 9 1 2 1 6 8 2 2 3 6 3
A17 2 2 8 7 5 7 7 3 5 7 0 0 3 1 7 5 4 2 3 8
A18 4 5 0 4 5 3 1 0 7 5 3 2 7 4 1 7 6 9 4 7
A19 5 5 3 9 0 3 7 8 8 2 6 3 1 2 0 5 2 6 1 8
['g', 'r', 'r', 'b', 'b', 'g', 'b', 'g', 'r', 'r', 'r', 'b', 'b', 'r', 'g', 'g', 'b', 'r', 'g', 'g']
A0 Group g
A1 Group r
A2 Group r
A3 Group b
A4 Group b
A5 Group g
A6 Group b
A7 Group g
A8 Group r
A9 Group r
A10 Group r
A11 Group b
A12 Group b
A13 Group r
A14 Group g
A15 Group g
A16 Group b
A17 Group r
A18 Group g
A19 Group g
dtype: object
11 """
12 # Display 0
13 iris = sns.load_dataset("iris")
14 species = iris.pop("species")
15 lut = dict(zip(species.unique(), "rbg"))
16 row_colors = species.map(lut)
17 """
18
19 # Libraries
20 import numpy as np
21 import pandas as pd
22 import seaborn as sns
23 import matplotlib.pyplot as plt
24
25 # Constants
26 N = 20
27 C = sns.color_palette("Spectral", n_colors=5, as_cmap=False)
28
29 # Create data
30 data = np.random.randint(low=0, high=10, size=(N, N))
31 colors = [np.random.choice(['r', 'g', 'b']) for i in range(N)]
32 series = pd.Series({'A%s'%i:'Group %s'%c for i,c in enumerate(colors)})
33
34 # Create DataFrame
35 df = pd.DataFrame(data,
36 index=['A%s'%i for i in range(N)],
37 columns=['A%s'%i for i in range(N)]
38 )
39
40 # Show
41 print(df)
42 print(colors)
43 print(series)
44
45 # Display 1
46 # ---------
47 # Create colors dictionary
48 col_colors = dict(zip(df.columns, colors))
49
50 # Show
51 g = sns.clustermap(df,
52 figsize=(5,5),
53 row_cluster=False, col_cluster=False,
54 #row_colors=col_colors,
55 col_colors=pd.Series(col_colors),
56 linewidths=0,
57 xticklabels=False, yticklabels=False,
58 center=0, cmap="vlag"
59 )
60
61 # Display 2
62 # ---------
63 # Create colors dictionary
64
65 col_colors = series.map({
66 'Group g': 'g',
67 'Group b': 'b',
68 'Group r': 'r'
69 })
70
71 # Show
72 g = sns.clustermap(df,
73 figsize=(5,5),
74 row_cluster=False, col_cluster=False,
75 #row_colors=col_colors,
76 col_colors=pd.Series(col_colors),
77 linewidths=0,
78 xticklabels=False, yticklabels=False,
79 center=0, cmap="vlag"
80 )
81
82 # Show
83 plt.show()
Total running time of the script: ( 0 minutes 0.513 seconds)