Step 1: Import required libraries
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
#matplotlib inline
Step 2: Load Flights datasets
flights = sns.load_dataset(‘flights')
flights.head()
flights.tail()
Step 3: Explore data using Heat Map
Please note that I am not displaying the resulting maps in this post. Please explore it yourself in your Jupyter notebook.
Before exploring Flights dataset with Heatmap, lets first analyze some random numbers using Heatmap:
numbers = np.random.randn(12, 15)
numbers
sns.heatmap(numbers)
sns.heatmap(numbers, annot=True) #to show actual values in the heatmap
sns.heatmap(numbers, annot=True, vmin=0, vmax=2) #to change the key value of heatmap, by default key varies from 0 and 1.
sns.heatmap(flights, cbar=False) #to hide the color bar
Now, lets jump to our Flights dataset. Lets pivot this dataset so that we have “year” on x-axis and “month” on y-axis.
flights = flights.pivot(‘month', ‘year', ‘passengers')
flights
sns.heatmap(flights)
sns.heatmap(flights, annot=True)
sns.heatmap(flights, annot=True, fmt='d') #format the annotation to contain only digits
sns.heatmap(flights, annot=True, fmt='d', linewidths=0.9) #add linewidth to heatmap
sns.heatmap(flights, annot=True, fmt='d', linewidths=0.9, cmap='RdBu') #add color map to heatmap to change the color
sns.heatmap(flights, annot=True, fmt='d', linewidths=0.9, cmap='summer')
sns.heatmap(flights, annot=True, fmt='d', linewidths=0.9, cmap='winter_r')
sns.heatmap(flights, annot=True, fmt='d', linewidths=0.9, cmap='coolwarm')
sns.heatmap(flights, annot=True, fmt='d', center=flights.loc[‘June', 1954]) #center color theme to a particular cell