CodeBlanks Logo Icon CodeBlanks

Summary - Visualization

In this group you learned scatter, histogram, and line plots.

Summary: visualisation

In this group:

  • fig, ax = plt.subplots(); drawing on the axis, savefig on the figure.
  • Scatter: relationship of two variables, classes with c=y; the truth-prediction plot.
  • Histogram: single-variable distribution, bins, np.histogram.
  • Line: validation and learning curves.
  • Box: quartiles, median, outliers.

Give every plot a title and axis names; the reader may not know the context.

Practice Two-panel figure

Open two axes with plt.subplots(1, 2): draw the histogram of x (bins=5) on the left and the scatter plot of y against x on the right, then save. Then print the correlation of x and y with round(..., 2).

Starter code

model.py
import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(1)
x = rng.normal(0, 1, 200)
y = 2 * x + rng.normal(0, 1, 200)

# buraya yaz

Sample solution

model.py
import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(1)
x = rng.normal(0, 1, 200)
y = 2 * x + rng.normal(0, 1, 200)

fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.hist(x, bins=5)
ax2.scatter(x, y)
fig.savefig("panel.png")
print(round(np.corrcoef(x, y)[0, 1], 2))

Expected output

0.9

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which plot best shows a feature's outliers and the difference between classes at the same time?

  • A single line plot
  • A pie chart
  • A table
  • A box plot per class Correct answer

Explanation: Side-by-side boxes show medians, spread and outliers together.

2 Create two axes side by side.

Question
fig, (ax1, ax2) = plt.subplots(1, ____)
Answer
fig, (ax1, ax2) = plt.subplots(1, 2)

Explanation: subplots(rows, columns) produces a grid of axes.

3 Match the questions with the plots.

Term Meaning
Are two variables related? Scatter
How are the values distributed? Histogram
How does the score change? Line
How do the groups differ? Box

All lessons in Beginner

13 Project: Classification

  1. Preparing data
  2. Training model
  3. Prediction
  4. Evaluation