A heatmap is a plot of rectangular data as a color-encoded matrix. As parameter it takes a 2D dataset. That dataset can be coerced into an ndarray.

This is a great way to visualize data, because it can show the relation between variabels including time. For instance, the number of fligths through the years.

Related course: Matplotlib Examples and Video Course

heatmap example

heatmap

The heatmap plot below is based on random values generated by numpy. Many parameters are possible, this just shows the most basic plot.

1
2
3
4
5
6
7
8
9
10
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

np.random.seed(0)
sns.set()
uniform_data = np.random.rand(10, 12)
ax = sns.heatmap(uniform_data, vmin=0, vmax=1)
plt.show()

heatmap

heatmap colors

The heatmap colors plot below uses random data again. This time it’s using a different color map (cmap), with the ‘Blues’ palette which as nothing but colors of bue. It also uses square blocks.

1
2
3
4
5
6
7
8
9
10
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

df = pd.DataFrame(np.random.randn(50).reshape(10,5))
corr = df.corr()

ax1 = sns.heatmap(corr, cbar=0, linewidths=2,vmax=1, vmin=0, square=True, cmap='Blues')
plt.show()

heatmap colors

heatmap data

The heatmap data plot is similar, but uses a different color palette. It uses the airline or flights dataset that’s included in seaborn.

1
2
3
4
5
6
7
8
9
10
11
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

sns.set()
flights = sns.load_dataset("flights")
flights = flights.pivot("month", "year", "passengers")
ax = sns.heatmap(flights)
plt.title("Heatmap Flight Data")
plt.show()

heatmap data