Get startedGet started for free

Matrix plots

1. Matrix Plots

The final category of plots we will discuss in this section are matrix plots. The heatmap is the most common type of matrix plot and can be easily created by Seaborn. These types of matrix plots can be useful for quickly seeing trends in a dataset. Let's learn how to construct these plots in Seaborn and use them to look at our bicycle rental data.

2. Getting data in the right format

Seaborn's heatmap() function expects the data to be in a matrix. To illustrate this process, we can look at the bicycle data to see how rentals vary by day of the week and month. The crosstab() function builds a table to summarize the data by the day and month. In this example, we also use the aggfunc argument to get the average number of rentals for each of the day and month combinations. This is a very useful capability available in pandas and makes it easy to plot a heatmap once it is in this format.

3. Build a heatmap

The heatmap translates the numerical values in the matrix into a color coded grid. In this example, the colors get lighter as the numbers increase. Using this approach, it is easy to see that the busiest days are Saturdays in June and September. The color code also illustrates that rental rates are higher in the warmer weather months from May through October.

4. Customize a heatmap

The display of the heatmap can be customized in multiple ways to present the most information as clearly as possible. This example illustrates a number of customizations. First, we use annot equals True to turn on annotations in the individual cells. The fmt option ensures that the results are displayed as integers. Next, we use a custom cmap of Yellow Green Blue to change the shading we use. By setting cbar equals False, the color bar is not displayed. Finally, passing a variable to linewidths puts small spacing between the cells so that the values are simpler to view. This example shows how changing a few parameters in Seaborn really changes the output of the plot.

5. Centering a heatmap

The final customization we will look at is centering the heatmap() color scheme. In this example, we center the colormap at the value stored for September and Saturday of 9 and 6 using the numeric indices for these values. The effect of this change is that overall color scheme is shifted towards yellows instead of blues.

6. Plotting a correlation matrix

One common usage for a heatmap is to visually represent the correlation between variables. pandas DataFrames have a corr() function that calculates the correlation between the values in the columns. The output of this function is ideally structured to be displayed as a heatmap. This visualization can be useful to understand which variables you might want to further study using regplot(). This plot shows that the total rentals are most highly correlated with the temp and casual variables.

7. Let's practice!

It’s time to put this into practice.