Heatmap-For-Blog

Heatmap

Heatmap depicts the two-dimensional data (matrix form) in the form of graph.

Data requirement:

Data can be in the form of:

  • Matrix such as correlation matrix
  • Or a pandas cross tabulated dataframe

Example:

  • Importing the data
heatmap-1
  • Cross-tabulate the data using pd.crosstab
heatmap-2
  • Plot the heatmap using seaborn library
heatmap-3
  • Add linewidths (width of line dividing each cell in the heatmap) and annotate (labeling)
heatmap-4
  • In this example, we have plotted the heatmap using the frequency of Incidence and damage combinations. Instead of count of incidence and damage class combinations, what if we want to plot the sum of the column ‘Values’?

For that, we can use values (which column to use?) and aggfunc (how to aggregate: “sum” or “mean” etc) options in pandas crosstab function.

data3=pd.crosstab(data.Incidence, data.Damage, values=data.Values, aggfunc = “sum”)

heatmap-5 heatmap-6
Scroll to Top