Multiline plots
It's fun to plot a line, but it's even more fun when we can plot more than one line on the same figure. This is really easy with Matplotlib as we can simply plot all the lines that we want before calling show()
. Have a look at the following code and screenshot:
In [1]: import matplotlib.pyplot as plt In [2]: x = range(1, 5) In [3]: plt.plot(x, [xi*1.5 for xi in x]) Out[3]: [<matplotlib.lines.Line2D object at 0x2076ed0>] In [4]: plt.plot(x, [xi*3.0 for xi in x]) Out[4]: [<matplotlib.lines.Line2D object at 0x1e544d0>] In [5]: plt.plot(x, [xi/3.0 for xi in x]) Out[5]: [<matplotlib.lines.Line2D object at 0x20864d0>] In [6]: plt.show()

Note how Matplotlib automatically chooses different colors for each line—green for the first line, blue for the second line, and red for the third one (from top to bottom).
Can you tell why a float value was used in line [5]? Try it yourself with an integer one and you'll see. The answer is that if divided by 3 (that is, by using...