- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
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
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
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')
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.
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.
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.
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.
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.
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.
Kickstart Your Career
Get certified by completing the courseGet Started