The world’s leading publication for data science, AI, and ML professionals.

How to Enhance Your Visualizations with a Matplotlib Mosaic

One of the coolest methods I have learned from Matplotlib


Introduction

Data visualization is very important. We could develop a lot just from this sentence, but I believe you already got the "picture". I am sure this is not the first time you hear this. And even if it is, I still assume it’s not a surprise, right?

A plot is worth a thousand observations.

If an image is worth a thousand words, I’d say that, for Data Science, a plot is worth a thousand observations.

Anyway, let’s move to what’s important here. The goal of this quick post is to show you the method subplot_mosaic(). This function is just amazing. Recently I was just browsing around and reading some Data Science related content when I saw it. I was just amazed by how easy it became to quickly create multiple plots in the same figure.

Let’s see how it is done in the next section.

The data to be used will be the dataset Tips, native from Seaborn package in Python.

import pandas as pd
import Matplotlib.pyplot as plt
import seaborn as sns

# Data
df = sns.load_dataset('tips')

Mosaic

Before Mosaic, there were other ways to create subplots. They are not difficult at all, but they are not as flexible as the subplot_mosaic() method. You can read the next post to know what I am talking about.

Plot Multiple Graphics in the Same Figure Using Python

The great contribution that the mosaic method gives us is the flexibility to create many designs with very little code. Look at the next image: if we want to reproduce that in matplotlib now, we will use the mosaic function.

Notice that we have 3 plots. Plot A will take one spot at the top left. Plot B will take one spot at the bottom left of the figure. Plot C will take two spots at top and bottom on the right-hand side. Thus, now, all we have to do is to translate that to Python, and you’ll see it’s really straightforward.

Let’s look at the code. We start creating a figure and then a mosaic variable. The mosaic variable can take lists or strings. I think it is easier to use strings with triple quotes and "draw" with text exactly how it will look like in the final plot figure. Notice that the use of ac bc in the code is in the same order and shape as the previous figure. By repeating c twice on the right side, I am telling matplotlib that I want the plot C to take those two spots of the figure. The rest of the code is just regular graphic creation code.

If you look at the variable mosaic , you will see this: {'a': <Axes: label='a'>, 'c': <Axes: label='c'>, 'b': <Axes: label='b'>}. So, it creates a dictionary, and we just need to point where each graphic will go now.

# Plot with Mosaic
fig = plt.figure(layout= 'constrained')
mosaic = fig.subplot_mosaic('''
                            ac
                            bc
                            ''')

# Plot A
mosaic['a'].bar(df.sex, df.tip, color='coral')

# Plot B
mosaic['b'].scatter(df.total_bill, df.tip, color='forestgreen')

# Plot C
mosaic['c'].boxplot(df.tip, patch_artist=True);

Here is the resulting visualization.

Ok. I hope I got your attention already.

We can create different layouts. Check this out.

# Plot with Mosaic
fig = plt.figure(layout= 'constrained', figsize=(12,6))
mosaic = fig.subplot_mosaic('''
                            aaa
                            bcc
                            ''')

# Plot A
mosaic['a'].bar(df.day, df.tip, color='coral')

# Plot B
mosaic['b'].boxplot(df.total_bill, patch_artist=True)

# Plot C
mosaic['c'].scatter(df.total_bill, df.tip, color='forestgreen');

Or yet, here’s another example with more graphics.

# Plot with Mosaic
fig = plt.figure(layout= 'constrained', figsize=(10,6))
mosaic = fig.subplot_mosaic('''
                            ab
                            ac
                            de
                            ''')

# Plot A
mosaic['a'].bar(df.day, df.tip, color='coral')

# Plot B
mosaic['b'].boxplot(df.total_bill, patch_artist=True)

# Plot C
mosaic['c'].scatter(df.total_bill, df.tip, color='forestgreen')

# Plot D
mosaic['d'].scatter(df.tip, df.total_bill, color='purple')

# Plot E
mosaic['e'].scatter(df['size'], df.tip, color='gold');

Setting Titles

But you might be asking yourself: what about chart titles?

Good question, I’d say! Let’s learn how to place them.

The first option is the simplest: we can add a single title for the entire image with one additional line of code.

# Adding a single title to the mosaic

plt.suptitle('''        -- P L O T S --
                - Top Left: Sum of tips by Sex -
                - Bottom Left: Tip by Total Bill -
                - Right: Boxplot of Tips - ''')

Alternatively, we may want to add one title per plot. Ergo, let’s create a loop that goes over each axes and each titles in a list and match them one by one. If we call mosaic.items(), we will see that dictionary with tuples, just like shown before: ('a', <Axes: label='a'>). To set the title, we want to access the Axes portion – the second item in the tuple – , and that’s why we use ax[1] in the code snippet in the sequence.

Here is how it can be done.

# Define Titles
titles = ['Sum of tips by Sex', 'Tip by Total Bill', 'Boxplot of Tips']

# One title per plot
for ax, g_title in zip(mosaic.items(), titles):
    ax[1].set_title(g_title, fontstyle='italic')

And this is the result.

Very nice, isn’t it?

Before You Go

I believe plotting multiple graphics in a single figure can be helpful for comparison, for presentation purposes. And having a method that makes it easier makes a lot of difference.

Remember the steps

  1. Create a figure
  2. Create a mosaic variable with subplot_mosaic() and use ”’triple quotes”’ to "draw" your mosaic. Write each graphic label as many times as needed to fill the spots in the mosaic.
  3. Add titles with plt.suptitle() or with a loop.

That’s all.

If you liked this content, follow me for more, here.

Find me on LinkedIn as well. And if you’re considering subscribing to Medium to read my posts and much more content, consider using this referral code, as you motivate me to keep creating content.

Join Medium with my referral link – Gustavo Santos

Reference

matplotlib.pyplot.subplot_mosaic – Matplotlib 3.7.1 documentation

Labelling subplots – Matplotlib 3.7.1 documentation


Related Articles