Skip to main content

Advanced Matplotlib: Subplots, Dual Axes, Styles, Annotations

Advanced25 min5 exercises100 XP
0/5 exercises

You know how to create a single chart. But real-world reports rarely have just one. A data analyst might need four charts side by side, each zoomed into a different slice of the same dataset. A researcher might need two completely different scales on the same chart — revenue in dollars on the left, growth percentage on the right.

This tutorial takes your matplotlib skills to the next level. You'll learn how to create multi-chart layouts with subplots, overlay two different scales with twin axes, annotate key data points, and apply professional styles that make your charts look polished.

How Do Subplots Let You Show Multiple Charts?

Think of subplots like a photo collage. You have one big frame (the Figure), and inside it you arrange multiple photos (the Axes) in a grid. Each "photo" is a completely independent chart with its own data, title, and labels.

The key function is plt.subplots(nrows, ncols). It returns a figure and an array of axes objects. For a 2x2 grid, you get four axes you can plot on independently.

Creating a 2x2 subplot grid
Loading editor...

When you have a single row or column of subplots, the axes array is 1D instead of 2D. You can force it to always be 2D with squeeze=False, but for simple cases, just index directly.

Single row of subplots
Loading editor...

How Do Twin Axes Show Two Different Scales?

Sometimes you need to show two datasets that have completely different units. Imagine revenue (in millions of dollars) and customer count (in thousands) on the same chart. If you plot them on the same y-axis, the smaller numbers would be squashed flat.

Twin axes solve this by adding a second y-axis on the right side of the chart. Each dataset gets its own scale, but they share the same x-axis so you can see how they relate over time.

Dual y-axis chart
Loading editor...

How Do You Annotate Key Data Points?

Annotations are like sticky notes on your chart. They point to specific data points and explain what's happening — a peak, a dip, a milestone. Good annotations turn a chart from "here's some data" into "here's what the data means."

Annotating data points
Loading editor...

The annotate() function takes two key coordinates: xy is where the arrow points (the data point), and xytext is where the text label sits. The arrowprops dictionary controls the arrow style.

How Do Built-in Styles Transform Your Charts?

Matplotlib's default style is... functional but plain. The good news is that matplotlib ships with dozens of built-in styles that change the entire look of your charts with a single line of code.

Available matplotlib styles
Loading editor...
Applying a style
Loading editor...

What Are Colormaps and When Do You Use Them?

A colormap is a mapping from numbers to colors. Instead of manually picking colors for 50 data points, you give matplotlib a range of values and it automatically assigns colors from a gradient. This is essential for heatmaps, contour plots, and any visualization where color represents a third dimension of data.

Colormaps in scatter plots
Loading editor...

Practice Exercises

Create a 1x3 Subplot Layout
Write Code

Create a figure with 1 row and 3 columns of subplots. Set the figure size to (12, 4). Set the titles of the three subplots to 'Sales', 'Profit', and 'Growth'. Call fig.tight_layout(), then print the shape of the axes array and the three titles.

Loading editor...
Build a Dual-Axis Temperature and Humidity Chart
Write Code

Create a chart with twin y-axes. Plot temperature on the left axis and humidity on the right axis using ax1.twinx(). Set the left y-label to 'Temperature (C)' and the right y-label to 'Humidity (%)'. Print both y-axis labels.

Loading editor...
Annotate the Maximum Value
Write Code

Given the prices list, find the index and value of the maximum price. Plot the data, then use ax.annotate() to label the peak point with the text 'Peak: $MAX' (where MAX is the actual max value). Also add a horizontal dashed line at the average price using ax.axhline(). Print the max value and the average (rounded to 1 decimal).

Loading editor...
Predict the Subplot Configuration
Predict Output

What will this code print? Think carefully about the subplot grid shape and the tight_layout call.

Loading editor...
Create a Color-Coded Scatter Plot
Write Code

Create a scatter plot where point color represents a third variable. Use numpy to generate 30 random x, y, and scores values (seed 42). Use ax.scatter() with c=scores, cmap='plasma', and s=80. Print the number of points and the colormap name from the scatter collection.

Loading editor...