Course 3.2Lesson 4 of 5
pandas draws graphs straight from a table with p.plot, p.plot.scatter, p.plot.bar and p.plot.hist, using matplotlib, so the code should import matplotlib.pyplot and call plt.show. The plot methods give the axes, and the axes methods from chapter 2 set the title, the labels, the scales and the grid. A scatter plot of the periods of the planets against their distances is a curve, and on a log-log graph it is a straight line, because a power law T equal to c times a to the power p has the logarithm log T equal to log c plus p times log a, and the slope of the line through the logarithms is the power: 1.5, as Kepler's third law says. A bar chart of a table or of a groupby result compares groups.
Try the code and the simulations beside the lesson.
After this lesson you can
You drew graphs in chapter 2 from arrays. A pandas table can draw itself: it has the methods plot.scatter, plot.bar, plot.hist and plot.line, which use matplotlib behind the scenes. They give back the axes, so you can use the object-style methods of chapter 2 to finish the graph. Always import matplotlib.pyplot as well, and call plt.show() at the end.
Predict first. Will the periods of the planets fall on a straight line when they are plotted against the distance?
The dots lie on a curve that gets steeper: not a straight line. The law of the planets is not linear. The inner planets are crowded in the corner, so that Mercury, Venus and Earth are hard to tell apart. This is a common problem with data that covers a wide range of sizes, and a log scale is the answer.
Suppose that . If you take the logarithm of both sides,
This is a straight line when is plotted against , and the slope is the power . So a log-log graph turns a power law into a straight line, and the slope tells the power.
Predict first. What slope do you expect from Kepler's third law, ?
This is a method, and not only a result about planets: whenever you suspect a power law (the area of a leaf against its length, the brightness of a lamp against the distance, the speed of a falling object against the height), plot the data on a log-log graph. If it is a straight line, the slope is the power.
Move p until the line runs through the dots. The fit in the code finds 1.5: the period grows as the distance to the power 1.5.
The left graph shows each planet. The right graph shows a summary of the groups. Which one is better depends on the question. For two kinds, the summary is easy to read. For the individual planets, the left graph shows the whole picture, including the fact that Jupiter is a little bigger than Saturn and that Uranus and Neptune are about the same.
matplotlib.pyplot. On this site, the plotting tools are loaded only when the code mentions matplotlib.x and y. They must be names of columns.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:
p.plot.scatter(x=..., y=...), p.plot.bar(...) and p.plot.hist(...) draw from a table. They return the axes.matplotlib.pyplot and call plt.show().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.