E ExamMaster

Engineering Python · NumPy & pandas Basics

Matplotlib Scatter

In Engineering Python because a two-column table is easier to read as points than as rows — scatter is the first plot after pandas.

A scatter plot places one point per row of two columns. The table below is five (hours, score) pairs — no invented study. `matplotlib.pyplot.scatter` draws those pairs.

  • Engineering Python
  • Medium level
  • 4 concepts

1Two columns become x and y

Each row is one point: the first column is the horizontal value, the second is the vertical value. Both columns must have the same length or the call fails.

The five pairs used here: hours 1,2,3,4,5 with scores 42,55,61,70,78. That is the whole dataset.

No diagram — the idea is carried by the prose, code block or coding lab.

Five (hours, score) pairs
hoursscore
142
255
361
470
578
For a scatter of hours vs score, what must be true of the two lists?
  1. They have the same length — one score per hours value
  2. They must both be strings
  3. Score must be longer than hours

Each point needs both coordinates.

2plt.scatter draws the points

`import matplotlib.pyplot as plt` then `plt.scatter(hours, score)` places the points. Label the axes so a reader knows which column is which.

The notebook runtime shows the figure as a cell output. Print the number of points so a stdout check can see the lab ran.

Takeaway

  1. Idea`import matplotlib.pyplot as plt` then `plt.scatter(hours, score)` places the points. Label the axes so a reader knows which column is which.

Scatter the five pairs

import matplotlib.pyplot as plt
hours = [1, 2, 3, 4, 5]
score = [42, 55, 61, 70, 78]
plt.scatter(hours, score)
plt.xlabel("hours")
plt.ylabel("score")
print(len(hours))
Which matplotlib function creates a 2D scatter plot of points (x, y)?
  1. plt.points(x, y)
  2. plt.dot_matrix(x, y)
  3. plt.scatter(x, y)
  4. plt.grid_plot(x, y)

matplotlib.pyplot.scatter(x, y) renders a scatter plot of markers at coordinates (x, y).

3Each point is one row

The leftmost point is hours=1, score=42. The rightmost is hours=5, score=78. Reading a scatter is reading the table again, with position instead of a grid.

A later ML lab may colour points by a third column. Here there are only two columns — do not invent a third.

Figure. The leftmost pair is hours=1, score=42. The rightmost is hours=5, score=78. Each point is one row of that table, read by position.

Read one point

  1. Lefthours=1, score=42.
  2. Righthours=5, score=78.

4Lab: scatter the five pairs

Draw the hours/score scatter and print how many points you plotted.

Figure. Scatter hours [1, 2, 3, 4, 5] against score [42, 55, 61, 70, 78]. That is five points — print len(hours) to show 5.

Takeaway

  1. IdeaDraw the hours/score scatter and print how many points you plotted.

Coding lab. Five-point scatter runs in the app, with checks on your output.

Notes

  • In Engineering Python because a two-column table is easier to read as points than as rows — scatter is the first plot after pandas.
  • Each row is one point: the first column is the horizontal value, the second is the vertical value. Both columns must have the same length or the call fails.
  • `import matplotlib.pyplot as plt` then `plt.scatter(hours, score)` places the points. Label the axes so a reader knows which column is which.

Exam traps & shortcuts

  • Run one cell at a time and read stdout before changing more lines.
  • Names are labels for values; rebinding a name does not rewrite old prints.

Recap

Two equal-length columns become points; scatter draws them; each point is a row.

Two columns become x and y
Each row is one point: the first column is the horizontal value, the second is the vertical value. Both columns must have the same length or the call fails.
plt.scatter draws the points
`import matplotlib.pyplot as plt` then `plt.scatter(hours, score)` places the points. Label the axes so a reader knows which column is which.
Each point is one row
The leftmost point is hours=1, score=42. The rightmost is hours=5, score=78. Reading a scatter is reading the table again, with position instead of a grid.
Lab: scatter the five pairs
Draw the hours/score scatter and print how many points you plotted.

Practise Matplotlib Scatter

Reading is free and needs no account. Practice, mocks and progress live in the app.

  • A 2-question practice set that ends the chapter
  • 2 quick checks with worked explanations
  • Timed mocks scored with the real marking scheme
  • Readiness tracked per topic, kept on your device
Continue with Google — freeNo card, no trial. Works offline once installed.