Draw

Matplotlib draws pictures from lists. You draw with pyplot. The short name is plt. plt.show opens the picture.

A line follows the list from left to right.

One list passed to plt.plot is the y coordinates. The x coordinates start at 0.

import matplotlib.pyplot as plt

plt.plot([3, 8, 1, 10, 5, 7])
plt.show()
  • The y coordinates are 3, 8, 1, 10, 5, and 7.
  • The x coordinates are 0, 1, 2, 3, 4, and 5.
  • The left end has x coordinate 0 and y coordinate 3.
A line through 3, 8, 1, 10, 5, and 7. The x coordinates run from 0 to 5.

Two lists name both coordinates. The first list is the x coordinate of each point. The second list is the y coordinate.

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [1, 4, 9, 16]
plt.plot(x, y)
plt.ylabel('squares')
plt.show()
  • The x coordinates are 1, 2, 3, and 4.
  • They do not start at 0, because the first list names them.
  • The squares are 1, 4, 9, and 16.
  • The right end is 16.
  • plt.ylabel('squares') names the y axis.
A line through the squares 1, 4, 9, and 16. The y axis reads squares.




In order to save your progress and be able to pass to the next lesson you need to LOGIN

×