1. Using PairGrid and pairplot
The next type of data-aware grid plots we will discuss are the PairGrid and pairplot. These plots are similar to the FacetGrid, catplot, and lmplots we discussed in the previous exercise because they allow us to see interactions across different columns of data. The difference with these plots is that we only define the columns of data we want to compare.
2. Pairwise relationships
Let's take a look at our Fair Market Housing data we looked at in a previous exercise. The PairGrid plot allows us to build plots that show the relationships between two data elements. In this example, we want to look at the relationships that might exist between the Fair Market Rent and Median Income of the tenants. The diagonals contain histograms of Fair Market Rent and Median Income. The other diagonal contains a scatter plot alternating which variable is on the x or y-axis.
3. Creating a PairGrid
The process for creating a PairGrid is similar to a FacetGrid in that we must create the grid, then map a plot type to the grid. The interesting point with this grid is that we do not define the row and column parameters. Instead we define the variables. In this case, the variables are the dataframe columns that we want to look at. The dataframe contains a Fair Market Rent column as well as a Median Income column, which we pass to the PairGrid. The next step is to let Seaborn know that we want to see a scatter plot visualization of the relationship between the two variables. Seaborn takes care of varying the x and y-axis for each of these plots.
4. Customizing the PairGrid diagonals
In the previous example, the diagonals contained straight lines, which are not very insightful. Fortunately, the PairGrid supports defining the type of plots that can be displayed on the diagonals. You can use the map_diag function to define the plotting function for the main diagonal. The map_offdiag function defines the other diagonal. In this case, we are looking at a histogram plot of the Fair Market Rent and Median Income. This view is much more useful than the previous scatter.
5. Pairplot
Pairplot is a shortcut function that simplifies the PairGrid. First, we define the variables like we did in the previous view. However, instead of mapping the plots, we can pass keywords to the function to specify the kind of plots for the main and off diagonals. The other nice aspect of this function is that it plots a regression line for us. This is a useful feature for visualizing potential relationships in the data.
6. Customizing a pairplot
Here is another example of how Seaborn can generate complex plots with very little code. To limit the data we are interested in, we filter the data to rentals with less than 3 bedrooms and assign hue to the bedrooms column. We also pass a keyword argument to reduce the alpha variable to 0-point-5. This makes the scatter plots transparent so that it is easier to see the observations. The other item to notice is that we passed in 3 variables, so this plot now contains a total of nine plots.
7. Let's practice!
The PairGrid and pairplot functions are useful tools for looking at the relationships between pairs of variables. Let's practice using these two plot types.