Get startedGet started for free

Making your visualizations efficient

1. Making your visualizations efficient

In this lesson, we'll focus on making our plots as information dense and efficient as possible without making them look too busy.

2. What is efficient?

Making a visualization more efficient means reducing the amount of effort the viewer needs to expend to understand the data and message being told. This can mean changing the layout of the visualizations to reduce context-switching, or trying to remove all the unnecessary ink on your plot. For instance, using facet-grid() so common axes tick values are only written once. It is important, however, to strike a balance in the efficiency of your visualizations. Make sure the desired message is not being sacrificed for the sake of ruthless optimization. In this lesson, we'll cover two quick and easy ways to improve the efficiency of your visualizations: plot stacking and legend hiding.

3. Combining multiple plots

Sometimes the story you want to tell in a dataset can be told in a single plot, but often you need to show more information than you can responsibly get onto a single figure. Luckily matplotlib and Seaborn make it rather easy to combine plots. We've already seen how to use Seaborn's FacetGrid() to build multiple plots faceted on some variable, but what if we want to combine different chart types with completely different axes? This is where matplotlib's subplots() comes in handy.

4. plt.subplots()

subplots() is a function in matplotlib that lets you set up a grid of plots that can then be filled in with whatever you desire. You tell the subplots() function how many rows and columns of plots you want and it returns multiple axes objects corresponding to the subplots in your grid. To place a Seaborn chart in a given subplot, you simply need to pass the corresponding axes object to the argument ax. Here we make two subplots and fill the first one with a line plot of pollution values over for multiple years. And the second plot with a bar chart with info on how many observations were made for each year.

5. Clear unnecessary legends

A good rule of thumb in visualization is the simpler, the better. Try to eliminate as much of the unnecessary 'stuff' drawn to the screen as possible. One way of doing this is to remove legends. While this sounds like a bad idea at first, keep in mind you must replace the legend with a better alternative. For instance, if you just have a few points drawn to the screen, you could directly label them with text, or if it fits, you can use a secondary plot as the legend.

6. How to remove legends

To remove the legend from our previous multi-plot example, we tell Seaborn to map years to the same colors in both charts by setting the same palette value for both. In addition, for the bar chart, you'll want to disable dodge to prevent the bars from becoming skinny. Last, we turn off the legends by calling legend_.remove() for the axes objects for both plots. The result is a plot that allows the reader to intuitively grasp what line belongs to what year with no explicit legend.

7. Let's practice

Always be on the lookout for new ways you can make your visualizations more efficient. In the meantime, let's practice plot stacking and legend hiding in the exercises.