06.b sns.heatmap for CRI v2

Plot rectangular data as a color-encoded matrix.

The generates a heatmap visualization for a dataset related to collateral sensitivity. It uses the Seaborn library to plot the rectangular data as a color-encoded matrix. The code loads the data from a CSV file, creates mappings for categories and colors, and then plots the heatmap using the loaded data and color maps. It also includes annotations, colorbar axes, category patches, legend elements, and formatting options to enhance the visualization.

16 # Libraries
17 import numpy as np
18 import pandas as pd
19 import seaborn as sns
20 import matplotlib as mpl
21 import matplotlib.pyplot as plt
22
23 from pathlib import Path
24 from matplotlib.patches import Patch
25 from matplotlib.colors import LogNorm
26 from matplotlib.patches import Rectangle
27
28 # See https://matplotlib.org/devdocs/users/explain/customizing.html
29 mpl.rcParams['axes.titlesize'] = 8
30 mpl.rcParams['axes.labelsize'] = 8
31 mpl.rcParams['xtick.labelsize'] = 8
32 mpl.rcParams['ytick.labelsize'] = 8
33
34 try:
35     __file__
36     TERMINAL = True
37 except:
38     TERMINAL = False

Lets load the data and create color mapping variables

43 # Load data
44 path = Path('../../datasets/collateral-sensitivity/sample')
45 data = pd.read_csv(path / 'matrix.csv', index_col=0)
46 abxs = pd.read_csv(path / 'categories.csv', index_col=0)
47
48 # Create dictionary to map category to color
49 labels = abxs.category
50 palette = sns.color_palette('colorblind', labels.nunique())
51 lookup = dict(zip(labels.unique(), palette))
52
53 # Create dictionary to map code to category
54 code2cat = dict(zip(abxs.antimicrobial_code, abxs.category))
55
56 # Create colors
57 colors = data.columns.to_series().map(code2cat).map(lookup)

Lets see the data

62 if TERMINAL:
63     print("\nData:")
64     print(data)
65 data.iloc[:7,:7]
AAMI AAMPC AAUG ACAZ ACELX ACIP ACOL
AAMI 1091.0 -0.000395 0.009905 0.006635 0.000565 0.008569 0.002179
AAMPC 5906.0 600.000000 -0.255642 0.070777 -0.142713 0.101858 -0.011118
AAUG 11962.0 5942.000000 7096.000000 -0.027724 0.206276 0.140313 0.027190
ACAZ 11642.0 5777.000000 11681.000000 1052.000000 0.455223 0.204145 -0.011411
ACELX 12005.0 5979.000000 93618.000000 11722.000000 7103.000000 0.233739 -0.013468
ACIP 11992.0 5974.000000 93507.000000 11714.000000 93568.000000 7115.000000 -0.011123
ACOL 9693.0 4768.000000 9726.000000 9526.000000 9758.000000 9745.000000 854.000000


Lets see the antimicrobials

69 if TERMINAL:
70     print("\nAntimicrobials:")
71     print(abxs)
72 abxs
name category acronym exists_in_registry antimicrobial_code
0 Amikacin Aminoglycosides AMIK True AAMI
2 Amp c markers NaN AMP_C True AAMPC
5 Augmentin NaN AUGM True AAUG
13 Cefotaxime Cephalosporins CEFO True ACTX
14 Cefoxitin Cephalosporins CEFOX True ACXT
16 Ceftazidime Cephalosporins CEFT True ACAZ
20 Cefuroxime Cephalosporins CEFU True ACXM
21 Cephalexin Cephalosporins CEPH True ACELX
24 Ciprofloxacin Fluoroquinolones CIPR True ACIP
29 Colistin sulphate Polypeptides COLI_SULP True ACOL
35 Ertapenem Meropenems ERTA True AERT
37 Esbl markers NaN ESBL_MARK True AESBL
44 Gentamicin Aminoglycosides GENT True AGEN
45 Imipenem Meropenems IMIP True AIMP
52 Mecillinam Penicillins MECI True AMEC
53 Meropenem Meropenems MERO True AMER
62 Nitrofurantoin NaN NITR True ANIT
81 Tazocin NaN TAZO True ATAZ
83 Temocillin Penicillins TEMO True ATEM
88 Tobramycin Aminoglycosides TOBR True ATOB
89 Trimethoprim NaN TRIM True ATRI


Lets create some variables.

78 # Create color maps
79 cmapu = sns.color_palette("YlGn", as_cmap=True)
80 cmapl = sns.diverging_palette(220, 20, as_cmap=True)
81
82 # Create triangular matrices
83 masku = np.triu(np.ones_like(data))
84 maskl = np.tril(np.ones_like(data))

Let’s display a heatmap

 91 # Draw (heatmap)
 92 fig, axs = plt.subplots(nrows=1, ncols=1,
 93     sharey=False, sharex=False, figsize=(8, 5)
 94 )
 95
 96 # Display
 97 r1 = sns.heatmap(data=data, cmap=cmapu, mask=masku, ax=axs,
 98             annot=False, linewidth=0.5, norm=LogNorm(),
 99             annot_kws={"size": 8}, square=True, vmin=0,
100             cbar_kws={'label': 'Number of isolates'})
101
102 r2 = sns.heatmap(data=data, cmap=cmapl, mask=maskl, ax=axs,
103             annot=False, linewidth=0.5, vmin=-0.7, vmax=0.7,
104             center=0, annot_kws={"size": 8}, square=True,
105             xticklabels=True, yticklabels=True,
106             cbar_kws={'label': 'Collateral Resistance Index'})
107
108 # Create patches for categories
109 category_patches = []
110 for i in axs.get_xticklabels():
111     try:
112         x, y = i.get_position()
113         c = colors.to_dict().get(i.get_text(), 'k')
114         #i.set_color(c) # for testing
115
116         # Add patch.
117         category_patches.append(
118             Rectangle((x-0.35, y-0.5), 0.8, 0.3, edgecolor='k',
119                 facecolor=c, fill=True, lw=0.25, alpha=0.5, zorder=1000,
120                 transform=axs.transData
121             )
122         )
123     except Exception as e:
124         print(i.get_text(), e)
125
126 # Add category rectangles
127 fig.patches.extend(category_patches)
128
129 # Create legend elements
130 legend_elements = [
131     Patch(facecolor=v, edgecolor='k',
132         fill=True, lw=0.25, alpha=0.5, label=k)
133             for k, v in lookup.items()
134 ]
135
136 # Add legend
137 axs.legend(handles=legend_elements, loc='upper center',
138     ncol=3, bbox_to_anchor=(0.5, -0.25), fontsize=8,
139     fancybox=False, shadow=False)
140
141 # Format
142 plt.suptitle('URINE - Escherichia Coli')
143 #plt.subplots_adjust(right=0.2)
144 plt.tight_layout()
145
146 # Show
147 plt.show()
URINE - Escherichia Coli

Out:

AMEC Invalid RGBA argument: nan
ATEM Invalid RGBA argument: nan

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

Gallery generated by Sphinx-Gallery