Seaborn Heatmap

Seaborn Heatmap

Seaborn is a statistical data visualization library based on Matplotlib. It provides a higher-level interface for drawing attractive and informative statistical graphics. One of the popular visualization plots in Seaborn is the heatmap which represents data values using colors.

A heatmap can be used to visualize a correlation matrix, display data in a 2D grid format, or even for simple annotations in a matrix.

Basic Heatmap:

  1. Setup:

    First, install and import necessary libraries:

    pip install seaborn 
    import numpy as np import seaborn as sns import matplotlib.pyplot as plt 
  2. Data:

    For demonstration, let's use random data:

    data = np.random.rand(10, 10) 
  3. Heatmap:

    sns.heatmap(data) plt.show() 

Heatmap of a Correlation Matrix:

A common use case for a heatmap is to visualize the correlation matrix of a dataset:

import seaborn as sns; sns.set_theme() tips = sns.load_dataset("tips") correlation_matrix = tips.corr() sns.heatmap(correlation_matrix, annot=True) plt.show() 

In this example, the annot=True argument allows the values of the matrix to be displayed on the heatmap.

Customizing the Heatmap:

  1. Changing the Color Map:

    sns.heatmap(data, cmap="YlGnBu") 
  2. Setting Minimum and Maximum Values:

    sns.heatmap(data, vmin=0, vmax=1) 
  3. Adding Separation Between Cells:

    sns.heatmap(data, linewidths=.5) 
  4. Using a Custom Color Map:

    cmap = sns.diverging_palette(220, 20, as_cmap=True) sns.heatmap(data, cmap=cmap) 
  5. Adjusting the Size:

    The size of the heatmap can be adjusted using Matplotlib:

    plt.figure(figsize=(10, 7)) sns.heatmap(data) 

Note:

Seaborn offers many other customization options for heatmaps, including masking certain portions, adding custom annotations, and adjusting the color bar. Refer to the official Seaborn documentation for a comprehensive overview of heatmap options and further examples.


More Tags

recaptcha-v3 local-storage lightbox decoder sonarjs mongodb-atlas propagation popen wikipedia graphite

More Programming Guides

Other Guides

More Programming Examples