Friday, August 26, 2022

How to plot using Matplotlib library?

To plot a function \(y(x)\) in matplotlib use matplotlib.pyplot.plot function. The general form of matplotlib.pyplot.plot function can be written as:
matplotlib.pyplot.plot(*args, scalex=True, scaley=True, data=none, **kwargs)
The scalex, scaley are bool values and the default value of both arguments is True. These parameters determine if the view limits are adapted to the data limits. The values are passed on to autoscale_view. **kwargs-> specify various properties such as line label, linewidth, antialiasing, and marker face color. However, to use the plot function you need to import the matplotlib library. The library is imported in python by typing in the following code:
import matplotlib.pyplot as plt
Plotting \(y\) versus \(x\) using matplotlib.pyplot.plot function after the library is imported can be written as:
plt.plot(x,y)
plt.show()
The last command line in the previous chunk of code is used to display all open figures in this case the plot(x,y) figure.
Example 1 - Plot the function \begin{eqnarray} y(x) &=& 3x+4 \end{eqnarray} using the matplotlib library.
Solution: In this example we will use the numpy library to create the list of input values \(x\) in range from 0 to 20 with step of 0.1. However, to create the list \(x\) the numpy library must be imported.
import numpy as np
x = np.arange(0,20.1,0.1)
print("x = {}".format(x))
The result of the previous chunk of code is given below.
x = [ 0.   0.1  0.2  0.3  0.4  0.5  0.6  0.7  0.8  0.9  1.   1.1  1.2  1.3
1.4 1.5 1.6 1.7 1.8 1.9 2. 2.1 2.2 2.3 2.4 2.5 2.6 2.7
2.8 2.9 3. 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4. 4.1
4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5. 5.1 5.2 5.3 5.4 5.5
5.6 5.7 5.8 5.9 6. 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9
7. 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 8. 8.1 8.2 8.3
8.4 8.5 8.6 8.7 8.8 8.9 9. 9.1 9.2 9.3 9.4 9.5 9.6 9.7
9.8 9.9 10. 10.1 10.2 10.3 10.4 10.5 10.6 10.7 10.8 10.9 11. 11.1
11.2 11.3 11.4 11.5 11.6 11.7 11.8 11.9 12. 12.1 12.2 12.3 12.4 12.5
12.6 12.7 12.8 12.9 13. 13.1 13.2 13.3 13.4 13.5 13.6 13.7 13.8 13.9
14. 14.1 14.2 14.3 14.4 14.5 14.6 14.7 14.8 14.9 15. 15.1 15.2 15.3
15.4 15.5 15.6 15.7 15.8 15.9 16. 16.1 16.2 16.3 16.4 16.5 16.6 16.7
16.8 16.9 17. 17.1 17.2 17.3 17.4 17.5 17.6 17.7 17.8 17.9 18. 18.1
18.2 18.3 18.4 18.5 18.6 18.7 18.8 18.9 19. 19.1 19.2 19.3 19.4 19.5
19.6 19.7 19.8 19.9 20. ]
Now the empty list \(y\) will be created which is the output. Then in a simple for loop, the \(y\) list will be fulfilled using the append built-in function for lists. The mathematical function, in this case, will be \(y=3x+4\). The code for generating \(y\) is given below.
y = []
for i in range(len(x)):
y.append(3*x[i]+4)
print("y = {}".format(y))
The last \(y\) will generate the following output.
y = [4.0, 4.3, 4.6, 4.9, 5.2, 5.5, 5.8, 6.1, 6.4, 6.7, 7.0, 7.3, 7.6, 7.9, 8.2, 8.5, 8.8, 9.1,
9.4, 9.7, 10.0, 10.3, 10.6, 10.9, 11.2, 11.5, 11.8, 12.1, 12.4, 12.7, 13.0, 13.3, 13.6, 13.9,
14.2, 14.5, 14.8, 15.1, 15.4, 15.7, 16.0, 16.3, 16.6, 16.9, 17.2, 17.5, 17.8, 18.1, 18.4, 18.7,
19.0, 19.3, 19.6, 19.9, 20.2, 20.5, 20.8, 21.1, 21.4, 21.7, 22.0, 22.3, 22.6, 22.9, 23.2, 23.5,
23.8, 24.1, 24.4, 24.7, 25.0, 25.3, 25.6, 25.9, 26.2, 26.5, 26.8, 27.1, 27.4, 27.7, 28.0, 28.2, 28.6,
28.9, 29.2, 29.5, 29.7, 30.1, 30.4, 30.7, 31.0, 31.2, 31.6, 31.9, 32.2, 32.5, 32.8, 33.1, 33.4, 33.7,
34.0, 34.3, 34.6, 34.9, 35.2, 35.5, 35.8, 36.1, 36.4, 36.7, 37.0, 37.3, 37.6, 37.9, 38.2, 38.5, 38.8,
39.1, 39.4, 39.7, 40.0, 40.3, 40.6, 40.9, 41.2, 41.5, 41.8, 42.1, 42.4, 42.7, 43.0, 43.3, 43.6, 43.9,
44.2, 44.5, 44.8, 45.1, 45.4, 45.7, 46.0, 46.3, 46.6, 46.9, 47.2, 47.5, 47.8, 48.1, 48.4, 48.7, 49.0,
49.3, 49.6, 49.9, 50.2, 50.5, 50.8, 51.1, 51.4, 51.7, 52.0, 52.3, 52.5, 52.9, 53.2, 53.5, 53.8, 54.0,
54.4, 54.7, 55.0, 55.3, 55.5, 55.9, 56.2, 56.5, 56.8, 57.0, 57.4, 57.7, 58.0, 58.3, 58.5, 58.9, 59.2,
59.5, 59.8, 60.0, 60.4, 60.7, 61.0, 61.3, 61.6, 61.9, 62.2, 62.5, 62.8, 63.1, 63.4, 63.7, 64.0]
To plot \(y\) versus \(x\) simply type in the following code.
plt.plot(x,y)
plt.show()
The result of the previous code is shown in Figure 1.
Figure 1 The graph of function \(y = 3x+4\)
The entire code used in this example is given below.
import numpy as np
import matplotlib.pyplot as plt
x=np.arange(0,20.1,0.1)
print("x = {}".format(x))
y = []
for i in range(len(x)):
y.append(3*x[i]+4)
print("y = {}".format(y))
plt.plot(x,y)
plt.show()
Example 2 Plot the functions \begin{eqnarray} y_1 &=& \cos x\\ y_2 &=& \cos 4x\\ \end{eqnarray}
Solution: In this example the function \(y_1=\cos x\) and \(y_2 = \cos 4x\) will be plotted using matplotlib library. As in previous case two libraries are required and the se are numpy and matplotlib library.
import numpy as np
import matplotlib.pyplot as plt
Next the x or argument of \(y_1\) and \(y_2\) function will be set in range from 0 to 20.
x = np.arange(0,20.1, 0.1)
For \(y_1\) and \(y_2\) empty list will be created and filled in simple for loop.
y1 = []; y2 = []
for i in range(len(x)):
y1.append(np.cos(x[i]))
y2.append(np.cos(4*x[i]))
To plot these two function simply use plot function twice.
plt.plot(x,y1)
plt.plot(x,y2)
plt.show()
The result is shown in Figure 2.
Figure 2 The graph of the functions \(y_1 = \cos x\) and \(y_2 = \cos(4x)\)

No comments:

Post a Comment