Plotting heatmap with
matplotlib/seaborn
February 2nd 2022
Method 1: matplotlib (auto)
import numpy as np
import matplotlib.pyplot as plt
data = np.random.random(( 12 , 12 ))
plt.imshow( data , cmap = 'autumn',interpolation = 'nearest' )
plt.title( "2-D Heat Map" )
plt.show()
https://www.geeksforgeeks.org/how-to-draw-2d-heatmap-using-matplotlib-in-python/
Method 2: seaborn (auto)
import numpy as np; np.random.seed(0)
import seaborn as sns; sns.set_theme()
uniform_data = np.random.rand(10, 12)
ax = sns.heatmap(uniform_data)
https://seaborn.pydata.org/generated/seaborn.heatmap.html
Method 2: seaborn (auto + label)
flights = sns.load_dataset("flights")
flights = flights.pivot("month", "year", "passengers")
ax = sns.heatmap(flights)
https://seaborn.pydata.org/generated/seaborn.heatmap.html
Method 2: seaborn (auto + label + annotation)
flights = sns.load_dataset("flights")
ax = sns.heatmap(flights, annot=True, fmt="d")
ax = sns.heatmap(flights)
https://seaborn.pydata.org/generated/seaborn.heatmap.html
Method 2: Two heatmaps
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib import gridspec
fig = plt.figure(figsize=(12, 10), dpi=300)
gs = gridspec.GridSpec(1, 2, width_ratios=[3, 1])
ax0 = plt.subplot(gs[0])
ax0 = sns.heatmap(terms, cmap=cmap, cbar=False)
ax1 = plt.subplot(gs[1])
ax1 = sns.heatmap(ground_truth, cmap=cmap, yticklabels=False,
cbar_kws={'shrink': 2.0, 'label': 'Quantile of term'})
Method 3: matplotlib (manual)
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon, Rectangle
from matplotlib.collections import PatchCollection
fig, ax = plt.subplots()
patch1 = []
for y_corr in reversed(range(11)):
for x_corr in range(11):
patch1.append(Rectangle((x_corr,y_corr),1,1))
p = PatchCollection(patch1, cmap='RdYlBu', alpha=0.6)
p.set_edgecolor('black')
# map color
color = df.values.flatten()
p.set_array(np.array(color))
ax.add_collection(p)
…
ax.set_yticklabels(...)
ax.set_ylabel('Query extension (bp)')
…
col = plt.colorbar(p, cax = cax, format=ticker.FuncFormatter(fmt))
col.ax.set_ylabel("Geometric mean of q-value")
…
ax.add_patch(Rectangle((5, 4), 1, 1, fill=False, edgecolor='lawngreen', lw=3))
Heatmap
Label
Color bar
Highlight rectangle
Method3: Heatmap with two colors
Heatmap with two colors
from matplotlib.patches import Polygon, Rectangle
for y_corr in reversed(range(10)):
for x_corr in range(10):
index1 = [[x_corr, y_corr], [x_corr + 1, y_corr + 1], [x_corr, y_corr + 1]]
patch1.append(plt.Polygon(index1))
Heatmap with two colors
added manually !!!

Plotting heatmap with matplotlib/seaborn

  • 1.
  • 2.
    Method 1: matplotlib(auto) import numpy as np import matplotlib.pyplot as plt data = np.random.random(( 12 , 12 )) plt.imshow( data , cmap = 'autumn',interpolation = 'nearest' ) plt.title( "2-D Heat Map" ) plt.show() https://www.geeksforgeeks.org/how-to-draw-2d-heatmap-using-matplotlib-in-python/
  • 3.
    Method 2: seaborn(auto) import numpy as np; np.random.seed(0) import seaborn as sns; sns.set_theme() uniform_data = np.random.rand(10, 12) ax = sns.heatmap(uniform_data) https://seaborn.pydata.org/generated/seaborn.heatmap.html
  • 4.
    Method 2: seaborn(auto + label) flights = sns.load_dataset("flights") flights = flights.pivot("month", "year", "passengers") ax = sns.heatmap(flights) https://seaborn.pydata.org/generated/seaborn.heatmap.html
  • 5.
    Method 2: seaborn(auto + label + annotation) flights = sns.load_dataset("flights") ax = sns.heatmap(flights, annot=True, fmt="d") ax = sns.heatmap(flights) https://seaborn.pydata.org/generated/seaborn.heatmap.html
  • 6.
    Method 2: Twoheatmaps import matplotlib.pyplot as plt import seaborn as sns from matplotlib import gridspec fig = plt.figure(figsize=(12, 10), dpi=300) gs = gridspec.GridSpec(1, 2, width_ratios=[3, 1]) ax0 = plt.subplot(gs[0]) ax0 = sns.heatmap(terms, cmap=cmap, cbar=False) ax1 = plt.subplot(gs[1]) ax1 = sns.heatmap(ground_truth, cmap=cmap, yticklabels=False, cbar_kws={'shrink': 2.0, 'label': 'Quantile of term'})
  • 7.
    Method 3: matplotlib(manual) import matplotlib.pyplot as plt from matplotlib.patches import Polygon, Rectangle from matplotlib.collections import PatchCollection fig, ax = plt.subplots() patch1 = [] for y_corr in reversed(range(11)): for x_corr in range(11): patch1.append(Rectangle((x_corr,y_corr),1,1)) p = PatchCollection(patch1, cmap='RdYlBu', alpha=0.6) p.set_edgecolor('black') # map color color = df.values.flatten() p.set_array(np.array(color)) ax.add_collection(p) … ax.set_yticklabels(...) ax.set_ylabel('Query extension (bp)') … col = plt.colorbar(p, cax = cax, format=ticker.FuncFormatter(fmt)) col.ax.set_ylabel("Geometric mean of q-value") … ax.add_patch(Rectangle((5, 4), 1, 1, fill=False, edgecolor='lawngreen', lw=3)) Heatmap Label Color bar Highlight rectangle
  • 8.
  • 9.
    Heatmap with twocolors from matplotlib.patches import Polygon, Rectangle for y_corr in reversed(range(10)): for x_corr in range(10): index1 = [[x_corr, y_corr], [x_corr + 1, y_corr + 1], [x_corr, y_corr + 1]] patch1.append(plt.Polygon(index1))
  • 10.
    Heatmap with twocolors added manually !!!