Skip to content

Multi Line Plots

Multi Line Plots

Multi-line plots are created using Matplotlib's pyplot library. In this section, we build upon the previous section where a plot with one line was created. To construct the data series for the multi-line plot, the NumPy package will be utilized. NumPy is a Python package useful for dealing with numerical data, particularly numerical data stored in matrices and arrays. Instead of defining a Python list of numbers to plot, the NumPy package is used to create an array of numbers. An array is a homogeneous data structure. Homogeneous data structures, like arrays, only contains one data type. Arrays are different from Python lists because Python lists are heterogeneous data structures. Heterogeneous data structures, like Python lists, can contain many different data types.

The NumPy** array below is a homogeneous data structure. The array only contains floating point numbers.

array([ 5.1,  0.004,  999.9,  0.0])

The Python list below is a homogeneous data structure. The list contains three different data types: an integer, a float and a string.

mylist = [ 4, 0.003, 'red' ]

The advantage of using NumPy arrays instead of Python lists is that mathematical operations can be run on an entire array at the same time. To run the same mathematical operation on each element in a Python list, a for loop (covered in a later chapter) is needed. When building plots, it is common to apply the same mathematical operation on every number in a range or apply the same mathematical operation on every value in a dataset. Therefore, NumPy arrays are quite useful for building plot. Before the NumPy package is used in a Python script, NumPy first needs to be imported. By convention numpy is imported as np.

import numpy as np

Once imported, NumPy functions, such as np.arange() and np.sin() function. A difference between NumPy's np.sin() function, compared to the function math.sin() from Python's standard library, is that np.sin() calculates the sine of every element in an array. math.sin(), from the Python Standard Library, only calculate the sine of one number.

NumPy's np.arange() function creates an array according to the following specifications:

np.arange(start, stop+step, step)

Where start is the first number in the array, stop is the last number in the array, and step is how far apart the numbers in the array are. Python counting starts at zero and ends at n-1. This means the second argument (stop + step) is not included in the final array. The final value in the array is stop. One extra number, beyond the last number you want in the array, needs to be specified. This type of interval is called a half-open interval.

An NumPy array starting at 0, ending at 10 and counting by 2's is coded by:

np.arange(0, 12, 2)

The result is:

array([ 0,  2,  4,  6,  8, 10])

Notice how 12 is included in the function call np.arange(0, 12, 2), but 10 is the last number in the array.

To construct a line plot of the sine function, import numpy and build an array that ranges from 0 to 4\pi in increments of 0.01. Then use np.sin() to calculate the sine of each value stored in the array. Finally, plot the results using Matplotlib's plt.plot() function.

In [1]:
import matplotlib.pyplot as plt
import numpy as np
# if using a Jupyter Notebook include
%matplotlib inline

x = np.arange(0, 4 * np.pi + 0.1, 0.1) y = np.sin(x) plt.plot(x, y)

plt.show()

To construct a plot which contains two lines, create two arrays of y-points and pass the arrays of y-points along with a corresponding set of x-points to the plt.plot() function.

plt.plot(x1,y1,x2,y2)

Where x,y1 creates the first line on the plot and x,y2 creates the second line on the plot.

In [2]:
import matplotlib.pyplot as plt
import numpy as np
# if using a Jupyter Notebook include:
%matplotlib inline

x = np.arange(0, 4*np.pi, 0.1) y1 = np.sin(x) y2 = np.cos(x) plt.plot(x, y1, x, y2)

plt.show()

A legend is added to the plot by calling plt.legend(). A list of strings is supplied to the plt.legend() function. The first set of points x, y1 is labeled by the first string in the list passed to plt.legend(). The second set of points x2,y2 is labeled by the second string in the list passed to plt.legend(). The x-axis and y-axis titles are specified by calling plt.xlabel() and plt.ylabel(). plt.title() adds the plot tile.
In [3]:
import matplotlib.pyplot as plt
import numpy as np
# if using a Jupyter Notebook include:
%matplotlib inline

x = np.arange(0, 4*np.pi, 0.1) y1 = np.sin(x) y2 = np.cos(x) plt.plot(x, y1, x, y2) plt.legend(['sine', 'cosine']) plt.xlabel('Angle (radians)') plt.ylabel('Sine or Cosine') plt.title('Sine and Cosine of Angles 0 to 4pi radians')

plt.show()