Module 5 · Matplotlib · Lesson 29 of 66

Advanced Matplotlib

Log scales, ticks, grids, twin axes, layouts, color maps and 3-D.

In this lesson · about 20 minutes

  • Use log scales, custom ticks and grids
  • Share a chart between two y axes
  • Build layouts with gridspec, and draw color maps and 3-D surfaces

You won't need these every day, but when you do, it's good to know they exist. Run each cell and change things to see what happens. The Matplotlib gallery has hundreds more examples.

Log scale

When values grow very fast, a logarithmic axis makes each step a multiple (1, 10, 100…) instead of a fixed amount, so the small values are still visible: ax.set_yscale('log').

Ticks, grid and spines

ax.set_xticks places the tick marks, and ax.set_xticklabels renames them (math in $…$ is drawn as formulas). ax.grid(True) adds grid lines, which take the same style options as lines. The spines are the four borders of the chart; you can recolor or hide each one.

Twin axes

To show two measures with different units on one chart, ax.twinx() makes a second y axis on the right that shares the same x axis.

Other chart types and text

step, bar and fill_between are a few more of the many types. ax.text(x, y, 'label') writes text at a spot in data coordinates.

Layouts

Beyond a simple grid, plt.subplot2grid lets a chart span several cells, and gridspec.GridSpec sets the relative widths and heights of rows and columns.

Color maps, contours and 3-D

For a function of two variables, a color map shows the third dimension as color (pcolor, imshow), and contour draws lines of equal value, like a topographic map. With projection='3d', the same data becomes a surface you can view from an angle.

Exercise

Switch to a log scale

Plot x against np.exp(x), put the y axis on a log scale, and turn on the grid.

Code that runs before yours
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 5, 11)
y = x ** 2

Exercise

Two y axes

Plot x ** 2 on ax1, then make ax2 = ax1.twinx() and plot x ** 3 on it. Label ax1's y axis 'area' and ax2's 'volume'.

Code that runs before yours
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 5, 11)
y = x ** 2

Module review0 of 3 correct

Question 1

1.You want to see how customer ages are spread out. Which chart?

Question 2

2.In fig.add_axes([0.1, 0.1, 0.8, 0.8]), what are the numbers?

Question 3

3.After fig, axes = plt.subplots(2, 2), how do you reach the bottom-right chart?

Quiz score: 0 of 3

Exercises solved: 0 of 2

Tip: answer every quiz question correctly first.

Noviq Academy's Data Science course is free and for learning. Python runs in your browser with Pyodide, which downloads about 20 MB the first time, plus a few more for scikit-learn. Your code and progress stay on this device. The salaries data is a sample of San Francisco's public pay records; the purchases, customers, housing, advertising and classified data are made up. The Titanic, tips, flights and iris data sets come from Seaborn's examples; the maps use public 2011 to 2014 US and world figures. The 911 data is a 20,000-call sample of Montgomery County, Pennsylvania's public 911 records; the movie ratings are the MovieLens 100K data from GroupLens Research; the reviews are a 3,000-review sample from the Yelp Dataset Challenge; the loans are public LendingClub data; the colleges are the College data from An Introduction to Statistical Learning; the bank notes, kyphosis and breast cancer data come from the UCI Machine Learning Repository. Spark and TensorFlow can't run in a browser, so those lessons use stand-ins that run here, and show the code for the real tools.