Lesson Overview: Crafting Your Data Storybook

Welcome to our storytelling session, where data comes alive on the pages of Matplotlib! Today, you'll become adept at weaving multiple data narratives together on a single canvas. This process is much like assembling a scrapbook, where every photo, or in this case, plot, adds depth to the story. By the end of this lesson, you'll know how to create a multi-plot narrative using layers on the same axis and within a single figure.

Understanding Subplots and Axes

Imagine you're building a scrapbook. Each page can hold multiple pictures, and you can decide where each photo goes. Subplots work similarly, helping us position multiple charts within a plot grid. We'll learn how to organize our data tales neatly on a page using subplots.

Here's a detailed example of creating subplots:

Let's decipher plt.subplot(1, 2, 1): 1, 2 defines a grid of one row and two columns, and the last 1 specifies the first column for our plot. This ensures your plots are arranged like photos on a scrapbook page, telling parts of the bigger story side by side.

Subplots: Result

The resulting figure looks like this:

Placing two plots near help us to gather data visualization in one place making it easier for the viewers to compare and connect pieces of information.

Students Performance Dataset

Let's consider a more meaningful dataset. Imagine we have this data for two students average marks and want to compare their performance with plots:

Plotting Students Performance

Here is how we will plot these graphs:

Notice a couple of things. Firstly, plt.ylim function ensures plots have equal y-axis limits, making the comparison more clear. Secondly, the figure is saved in the fig variable, which lets us add a main title using fig.suptitle function. Finally, we use the new plt.subplots_adjust function with a variable wspace parameter to ensure there is enough place between the subplots.

Plotting Students Performance Result

Here is the result:

Having two plots side-to-side plots lets us both investigate individual graphs and easily compare them to each other.

Layering Plots on the Same Axis

There is another way to compare these plots.

To tell a rich story, we often combine multiple elements on the same page, like overlaying transparent leaves over a photo. Similarly, we can overlay plots on the same axis in Matplotlib, comparing different datasets directly.

Combining two plots on one axis is easy: call the plt.plot function twice!

Result of the Plots Overlaying

Here is the result. Check out how we can compare two students' performances:

Notice that the plt.legend() function acts as a caption beneath our photo, helping readers understand which element is within the story's overlay.

Wrapping Up: Your Data Storybook Complete

Bravo! You're now equipped to craft a multi-layered data storybook with plots on the same axis and diverse visuals collected in one figure. Just as a scrapbook contains different pictures and embellishments on a page, using plt.subplots() and plt.figure(), you've learned to arrange different data plots to create multifaceted visual narratives in Python with Matplotlib.

As we close this chapter, get ready to apply your skills in the next round of practice exercises. By creating your own data stories, you'll solidify your understanding of these techniques and begin to see the bigger picture in your data. Your journey into eloquent data storytelling has only just begun!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal