1. Additional catplot() options
To take the visualizations we have worked on a step further, we will update their titles, axes, and colors, as well as create multiple plots at the same time.
2. Difficulties with categorical plots
Summarizing data by category is a great start, but trying to visualize multiple categories can be difficult.
Take this example, which counts the number of hotels in our Las Vegas reviews dataset by traveler type and user continent. Although there is nothing overtly wrong with the graphic, it pushes users to compare the count across continents, when the more interesting information may be the count within the continents. Instead of creating six different plots, one for each continent, we need a better option.
3. Using the catplot() facetgrid
We can create a better visualization by replicating the same graphic several times in a single plot. Before we look at the code, let's explore the output.
Here we see the same information for number of hotel reviews by traveler type and user continent, but the emphasis is on the within continent information. Let's take a look at how to do this.
4. Using different arguments
The catplot we just saw was created using the following code. We have specified the x-axis to be based on the categorical column traveler type and have set the type of the graphic to count. We have set the parameter col to be user continent, which tells Seaborn to create a catplot of each category in user continent. We also set col-wrap to be 3, which makes the visualization go to the next line, or wrap itself, after every 3rd graphic. And finally, we went ahead and changed the colors of the graph using Seaborn's color-palette function. Here we have used Set1 as the color palette, although Seaborn has a ton of different options that can be found using the link on your screen. Some of the most commonly used are set2, tab10, and paired.
5. One more look
Let's take a look at this graphic one more time. Again, notice the emphasis is on the counts within each category of user continent and we have six clean graphics, all in one plot. The text may be a little small, but we already know how to fix that issue!
6. Updating plots
To really finish off creating our graphics, we need to learn how to adjust some basic options. To do this, we need some help from matplotlib. We will start by saving our seaborn graphic as an object, ax, and then using matplotlib to update specific items of the object.
We can add a title by using fig-dot-suptitle on our saved object. We can add axis labels using set-axis-labels. The x axis label comes first, followed by the y axis label. In some cases, the title is displayed over top the graphic. We can fix this by setting the top of the actual plot to be at 90% of the full graphic. This is achieved using matplotlib's subplots-adjust function.
Here is the finalized code. We have saved our graphic as an object, set a title and axis labels, and also fixed the height of the plot, so the title is displayed properly.
7. Finished product
And here is the final graphic. We have broken down the number of reviews by the user continent and reviewer type. Although a small sample size, Asia - the bottom middle graphic - had more reviews from traveling families than for couples. This is quite different than continents such as North America, Europe, or Oceania.
8. catplot() practice
Let's spend some time working on a couple of examples.