Display the Pandas DataFrame in Heatmap style


Pandas is a powerful data analysis library that offers a wide range of functions to work with structured data. One of the most popular ways to represent data is through a heatmap, which allows you to visualize data in a tabular format with colours representing the values. In this article, we will explore how to display a Pandas DataFrame in a heatmap style using the Seaborn library.

In data analysis and visualization, a heatmap is a popular tool used to display the relationship between variables in a tabular dataset. Heatmaps represent data as a grid of coloured squares, with each square colour indicating the relative value of the data in that cell. Heatmaps are commonly used in a variety of fields, including biology, finance, and social science.

In this article, we will explore how to display a Pandas DataFrame in a heatmap style using the Seaborn library.

Installing Seaborn

Seaborn is a Python library used for data visualization based on matplotlib. It provides a high-level interface for creating informative and attractive statistical graphics. To install Seaborn, you can use the following command in your terminal −

pip install seaborn 

Importing Libraries

After installing Seaborn, we need to import it along with other required libraries. We will use Pandas to load and manipulate our data and Matplotlib to display our heatmap.

import pandas as pd
import seaborn as sns 
import matplotlib.pyplot as plt 

Loading Data

For this example, we will use a dataset of a fictional company's employee performance. The dataset contains information about the employees' age, gender, department, salary, and performance rating.

df = pd.read_csv('employee_performance.csv') 

Creating Heatmap

Now that we have our dataset loaded, we can create a heatmap using the heatmap function of the Seaborn library. We will pass our DataFrame to this function, and Seaborn will automatically generate a heatmap.

sns.heatmap(df) 
plt.show() 

This code will produce a heatmap of our DataFrame with default settings. By default, Seaborn will use a color gradient to represent the values of the cells in our DataFrame. The darker the color, the higher the value. We can also customize our heatmap using various parameters.

Customizing Heatmap

Colormap

We can change the color palette of our heatmap using the cmap parameter. Seaborn provides various color palettes, and we can choose the one that suits our data. For example, we can use the Blues color palette by passing it to the cmap parameter.

sns.heatmap(df, cmap='Blues') 
plt.show() 

This code will produce a heatmap using the Blues color palette.

Annotations

We can also annotate our heatmap with the actual values of the cells. We can use the annot parameter to achieve this. By default, Seaborn will display the actual values of the cells. We can also format these annotations using the fmt parameter.

sns.heatmap(df, cmap='Blues', annot=True, fmt='.2f') plt.show() 

This code will produce a heatmap with cell annotations formatted with two decimal places.

Axis Labels

We can also add axis labels to our heatmap using the xlabel and ylabel parameters.

sns.heatmap(df, cmap='Blues', annot=True, fmt='.2f') 
plt.xlabel('Features') plt.ylabel('Employees') 
plt.show() 

This code will produce a heatmap with axis labels.

Conclusion

In this article, we explored how to display a Pandas DataFrame in a heatmap style using the Seaborn library. We learned how to customize our heatmap using various parameters such as colormap, annotations, and axis labels. By visualizing our data in a heatmap style, we can quickly identify patterns and relationships between variables, making it easier to derive insights from our data.

Updated on: 31-Jul-2023

879 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements