In python, we can use python seaborn library to draw a heatmap graph. In this tutorial, we will use some examples to show you some tips when using it.
sns.heatmap() syntax
sns.heatmap() is defined as:
seaborn.heatmap(data, *, vmin=None, vmax=None, cmap=None, center=None, robust=False, annot=None, fmt='.2g', annot_kws=None, linewidths=0, linecolor='white', cbar=True, cbar_kws=None, cbar_ax=None, square=False, xticklabels='auto', yticklabels='auto', mask=None, ax=None, **kwargs)
We should notice:
data: it usually be a 2-D numpy array.
cmap: Change the color of heatmap.
annot: False or True. Displaying value or not in heatmap.
Here is an example:
import numpy as np import matplotlib.pyplot as plt import seaborn as sns data = np.random.rand(10, 5) ax = sns.heatmap(data = data) plt.show()
Here data is a 2-D array, the shape of which is 10 * 5
Running this code, you will get this graph.
The shape of data is 10 * 5. From the result, we can find:
row = 10, which is the y axis.
column = 5, which is x axis.
Change the color of heatmap
We can set parameter cmap to implement it.
Here is an example:
import numpy as np import matplotlib.pyplot as plt import seaborn as sns data = np.random.rand(10, 5) ax = sns.heatmap(data = data, cmap="YlGnBu") plt.show()
Run this code, you will get this result:
Display value in heatmap
Here is an example:
data = np.random.rand(10, 5) ax = sns.heatmap(data = data, annot = True) plt.show()
Run this code, we will get this heatmap.