Course 1.2Lesson 2 of 5
To draw the graph of a function, make an array of x values with np.linspace, calculate y from the formula for the whole array, and plot the two. More points give a smoother curve, because plt.plot joins the points with straight pieces, so a curve needs about 100 or more. Several curves can share one graph with labels and a legend. Angles for sine and cosine are changed to radians with np.deg2rad. plt.xlim and plt.ylim set the range, plt.axhline and plt.axvline draw the axes, plt.xticks chooses the ticks, plt.plot with a marker shows special points, and plt.annotate writes a note at a point. The meeting points of two graphs are the solutions of an equation, and np.roots finds the roots of a polynomial.
Try the code and the simulations beside the lesson.
After this lesson you can
In your maths class, to draw you made a table of values of and , plotted the points, and joined them. With NumPy, you do the same, but with hundreds of points and no effort: you make an array of values, calculate the array of values with the formula, and plot them.
Predict first. Which line will look smoother, the one with 200 points or the one with 5 points?
np.linspace(-3, 3, 200) gives 200 values of from to . And many ** 2 is the 200 values of . (We use ** for the power.)plt.plot joins the neighbouring points with straight lines. With 5 points, the picture has corners, because it is made of 4 straight pieces. With 200 points, the pieces are so short that the eye sees a smooth curve.Notice the title: "y = x squared". In a title, you cannot easily write the power, so it is written in words, or with a short form such as x^2 as a text. The title is only a text.
For a smooth graph, 100 to 200 points are plenty. They cost nothing, because NumPy works them out in one line.
The places where two graphs meet are the solutions of an equation. The parabola and the line meet where . Move everything to one side: .
Predict first. Solve by factorising: . Where do the graphs meet? Then run it.
np.roots([1, -2, -3]) gives the roots of the polynomial : the coefficients are listed from the highest power. The answer is the two numbers, 3 and -1, as you found by factorising. (Chapter 3 returns to this function. Rounding to 6 decimals removes the tiny errors in the last digits.)plt.plot(roots, 2 * roots - 1, "ro") draws two red dots (r is red, o is a round marker) at the meeting points. A dot is a plot of its own, with only markers.plt.annotate(text, (x, y), ...) writes a note near a point. xytext=(8, -14) moves the text 8 points to the right and 14 points down from the point, so that the note does not sit on the line.plt.xlim(-4, 5) and plt.ylim(-8, 14) set the range that is shown. Without them, matplotlib picks a range. A good range shows the important part of the graph: here, both meeting points and the shape of the parabola.The graph and the equation say the same thing in two ways. The graph shows where the solutions are. The calculation gives them exactly.
np.sin. Use np.deg2rad first.xlim and ylim.y = x^2 in a calculation. In Python, the power is **.np.roots. The coefficients start with the highest power.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:
x = np.linspace(...), y = formula(x), plt.plot(x, y).np.deg2rad before np.sin and np.cos.plt.xlim, plt.ylim, plt.axhline, plt.xticks and plt.annotate control and explain the picture.np.roots solves a polynomial equation.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
np.roots([1, -5, 6]) gives?"ro" mean as a format?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.