So far we have created simple plot using matplot.pyplot.plot() funcion. In previous examples the line plot was used to create a line between the points and these points were define in form of x and y coordinates. For more information please check Getting started with basic Matplotlib plots.
In this tutorial we will create a line plot with two lines and these lines will connect the data points defined in the form of x and y coordinates stored in x and y1 lists for first line, and in x and y2 lists for the second line. It can be noticed that both lines used the same x coordinate values.
\begin{eqnarray}
x &=& [1,2,3,4,5,6]\\ \nonumber
y_1 &=& [2,4,6,8,10,12]\\ \nonumber
y_2 &=& [3,6,9,12,15,18]
\end{eqnarray}
The first step is to load required libraries. As in previous cases the only required library is the matplotlib and the module is pyplot.
import matploltib.pyplot as plt
The next step is to defined lists of x, y1, and y2 variables. These are coordinates for two line plots that will be plotted.
x = [1,2,3,4,5,6]
y1 = [2,4,6,8,10,12]
y2 = [3,6,9,12,15,18]
The next step is to define the figure size and as defined in previous examples we will use the figure size of 12 by 8 inches.
plt.figure(figsize=(12,8))
To plot the two lines on the same graph we will have to use plt.plot() function twice. The arguments inside the first plot function will be x and y1 lists and in second x and y2. Since there are two lines plots on the same graph it is a good practice to distinguish them so the third argument in each plot function will be label. The label in the first plot function will be $y = 2x$ and the label of the second plot function will be $y = 3x$.
These labels are needed to create a plot legend which shows how to distinguish each of the curves in the plot. The plot legend in Matplotlib is a box that labels the different elements or data series in a plot, such as lines, markers, or bars. It helps to distinguish between multiple data sets by showing which color or style corresponds to each label. This makes the plot easier to interpret and understand. \newline
The remaining elements of the multiple line plot are to define the title, xlabel, ylabel, legend, grid and finally to show the plot. The title plt.title() is used to define the title of the plot, the plt.xlabel() and plt.ylabel() are used to define the labels of x and y axes. The plt.legend() is a new function (it was not used in previous examples) and to show the plot legend it is enough to define it as plt.legend(). The customization and position of the plot legened will be explained in future examples. It should be noted that if the labels are not defined inside the plot functions the legend function will have no effect on the matploltib plot which means that legend would not exist in the multi-line plot.\newline
The remaining functions are grid and show i.e. the grid is used to show the grid inside the matplotlib plot and the show function is used to display the matplotlib multi-line plot.