Beginning the Journey

Welcome to our lesson on Heat Maps for Monthly Data Analysis. In this session, we'll dive deeper into the more complex visualizations using the Seaborn library to plot heat maps.

Heat maps are a superb tool for displaying multivariate datasets in a two-dimensional image. They visually represent data through colors, where different color gradients represent different values. This is very useful in fields like Data Science, as heat maps are powerful tools for exploring and understanding patterns in a given dataset.

In our context of analyzing air travel data, what if we could find out how the monthly passenger count has fluctuated over the years? Which month or year had the highest passenger count? Does the count exhibit a pattern or trend? Heat maps are a great tool to answer these questions, and we'll learn to do exactly that in this lesson.

Delving Into Heat Maps

Heat maps are generated using the Seaborn library, which builds on Matplotlib and integrates seamlessly with pandas data structures. Let's start by developing a heat map for monthly passenger trends in air travel.

We start by loading up the flights dataset, as before:

import seaborn as sns

# Load the dataset
flights = sns.load_dataset('flights')

Since our interest is on a year-by-year and month-by-month basis, a pivot table fits our requirements best. The pivot table will have months as rows, years as columns, and passenger counts as the cell values. Python's pandas library makes creating this pivot table straightforward:

# Pivot the dataset
flights_pivot = flights.pivot(index="month", columns="year", values="passengers")
print(flights_pivot)
"""
year   1949  1950  1951  1952  1953  1954  1955  1956  1957  1958  1959  1960
month                                                                        
Jan     112   115   145   171   196   204   242   284   315   340   360   417
Feb     118   126   150   180   196   188   233   277   301   318   342   391
Mar     132   141   178   193   236   235   267   317   356   362   406   419
Apr     129   135   163   181   235   227   269   313   348   348   396   461
May     121   125   172   183   229   234   270   318   355   363   420   472
Jun     135   149   178   218   243   264   315   374   422   435   472   535
Jul     148   170   199   230   264   302   364   413   465   491   548   622
Aug     148   170   199   242   272   293   347   405   467   505   559   606
Sep     136   158   184   209   237   259   312   355   404   404   463   508
Oct     119   133   162   191   211   229   274   306   347   359   407   461
Nov     104   114   146   172   180   203   237   271   305   310   362   390
Dec     118   140   166   194   201   229   278   306   336   337   405   432
"""

Now that we have our pivot table, we can create a heat map. We use Seaborn's heatmap() function, passing in our pivot table as an argument:

# Plot a heatmap
sns.heatmap(flights_pivot)

plt.show()

visualization

This heat map immediately provides insight into the passenger count over the years. The color gradient (warmer for higher values, cooler for lower values) makes it easy to spot patterns and trends over time.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal