Course 1.2Lesson 3 of 5
Different questions need different graphs. A line graph shows how one measurement changes with another, such as time. A scatter plot, plt.scatter, shows two measurements of each person or object as one dot, and colour or size can show a third measurement. np.corrcoef gives the correlation, a number between minus 1 and 1, and a strong correlation is not proof that one thing causes the other. A bar chart, plt.bar or ax.bar, compares amounts in separate groups, and ax.bar_label writes the numbers. A histogram, plt.hist, shows how many measurements fall in each bin, and it shows the centre and the spread of a measurement. np.histogram gives the same counts as numbers.
Try the code and the simulations beside the lesson.
After this lesson you can
The kind of graph depends on the question.
| The question | The graph |
|---|---|
| How does a measurement change in time or with another quantity? | line graph (plt.plot) |
| Is there a link between two measurements of the same things? | scatter plot (plt.scatter) |
| How do separate groups compare? | bar chart (plt.bar) |
| How are the repeated measurements of one quantity spread? | histogram (plt.hist) |
Predict first. The ten students are taller when they are older. Will the dots go up or down to the right?
c=age colours each dot by the age, with the colour map "viridis", and s=70 sets the size of the dots. plt.colorbar(label=...) shows what the colours mean. With the colour, the graph shows three measurements at once.np.corrcoef(height, weight) gives a table of correlations. The number at [0, 1] is the correlation coefficient between the two. It is always between and . A value near 1, such as the 0.993 here, means that the dots lie almost on a rising line. A value near 0 means no link, and a value near means a falling line.A warning that every scientist keeps in mind: correlation is not cause. Taller students also tend to be older, and older students weigh more. When two things go up together, there may be a third thing behind both, or it may be by chance. The graph shows that two measurements are linked, but it does not say why.
Set the slope to 0: the correlation printed by the code drops close to 0, because there is no trend left, only the scatter.
Do not use a line to join bars, and do not join the dots of a scatter plot. The bars are separate groups, and a line between them would suggest a change between the groups that does not exist.
Predict first. The rod is 10 cm long. When it is measured 200 times, will every measurement give 10.00?
rng.normal(loc=10.0, scale=0.2, size=200) makes 200 random numbers around 10.0 with a typical spread of 0.2. We use random numbers here to imitate the measurements of an experiment. The next chapters explain how random numbers work.np.histogram(lengths, bins=8) sorts the measurements into 8 bins (equal ranges) and counts how many fall in each. It gives the counts and the edges of the bins. The counts add up to 200.plt.hist(lengths, bins=8) draws these counts as bars, and the bars touch, because the bins follow each other without a gap. This is different from a bar chart of separate groups.plt.axvline(lengths.mean(), ...) draws a vertical dashed line at the mean.How many bins? Too few, and the shape is lost. Too many, and each bin has only a few measurements and the shape is jagged. For 200 measurements, 8 to 15 bins is a good choice. Try 4, and then 50, to see the difference.
Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
plt.scatter(x, y, c=..., s=...) and plt.colorbar show a third measurement.np.corrcoef(x, y)[0, 1] is between and . Correlation is not cause.ax.bar_label writes the values on the bars. np.histogram gives the counts as numbers.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.