Module 5 · Matplotlib · Lesson 28 of 66

Scatter plots, histograms and box plots

The everyday chart types beyond the line chart.

In this lesson · about 12 minutes

  • Show two variables with a scatter plot
  • Show a distribution with a histogram
  • Compare groups with a box plot and a bar chart
ChartCodeUse it to…
Scatterplt.scatter(x, y)See how two numbers relate
Histogramplt.hist(data, bins=20)See how one number is spread out
Box plotplt.boxplot(data)Compare the middle, spread and outliers of groups
Bar chartplt.bar(names, values)Compare amounts across categories

Later you'll meet Seaborn, which makes many of these statistical charts prettier with less code, but it's all Matplotlib underneath.

Exercise

Sales by month

Make a bar chart of sales by months with ax.bar, and title it 'Sales by month'.

Code that runs before yours
import matplotlib.pyplot as plt
months = ['Jan', 'Feb', 'Mar', 'Apr']
sales = [120, 98, 143, 160]

Exercise

A histogram with 10 bins

Draw a histogram of ages with exactly 10 bins.

Code that runs before yours
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(7)
ages = np.random.randint(18, 80, 300)

Exercises solved: 0 of 2

Tip: solve each exercise first. Press Check to test your code.

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.