How to plot sine wave in Python? | applied electronics engineering


How to plot sine wave in Python?

By Applied Electronics - Monday, May 30, 2016 No Comments
Python is a programming language that defines basic data types, functions, program structure rules and regulation. It does not have the inbuild functions or modules that allows you to plot graphics. What you must do is to install a module called matplotlib.

Once you have installed matplotlib, you can then use the various matplotlib plotting functions defined in the matplotlib such as plot( ), show( ) etc to manipulate the graphics. However to be able to plot a sine wave in python requires you to have calculate the sine function for various coordinates say t for time. Thus you must have sin function first, calculate the values of sin for each time t. Then you must pass these two t and sin(t) values as arguments to the matplotlib function such as plot(t, sin(2*pi*f*t) ). What it means is that you also need modules that has support for the function such as sine that you want to calculate since sin( ) function is not available in default python language.

Thus you also need to install and import modules such as scipy, numpy or math that does have sin( ) function or whatever function you want to plot.

Usually, the math module comes preinstalled with Python so let us first show how to plot the sine wave using math module. Then we will see how to use the function from Numpy. Then you can also look into scipy module functions if you are further interested.

Using Math module

[In] import math

[In] import matplotlib.pyplot as plt

[In] T = list(range(101))

[In] t = [k/len(T) for k in T]

[In] y = [math.sin(2*math.pi*tt) for tt in t]

[In] plt.plot(t,y)
[Out] [<matplotlib.lines.Line2D at 0xd862090>]

What we did in this case is that we first import the math module because sin function is defined there. Then we imported another module matplotlib.pyplot because the functions such as plot( ) are defined there. First we created an linear step numbers and stored them in the list T. So T would be 0,1,2,3....100. We created another list by dividing each number in T= 0,1,2,3...100 by the length of T. t would have 0.0, 0.01, 0.02 and so on upto 0.99 close to 100. So we will again have 100 samples. Thus these are sampling instances. Then for each sampling instant we calculate the sine value and store them as list in y. Finally, we plot the y values against the sampling instances and we get the sine wave plot.

Using Numpy

Another method is to use the numpy method. The difference is that in Numpy, the sin function calculates the sine values iteratiblly for the sampling instances. Thus unlike in case of sin of math module, we do not have to calculate sine values using list comprehension method. In Numpy case, we just have to pass in the list as sin function argument.

In] import numpy as np

[In] import matplotlib.pyplot as plt

[In] T = list(range(101))

[In] t = [(2*np.pi*k)/len(T) for k in T]

[In] y = np.sin(t)

[In] plt.plot(t,y)
[Out] [<matplotlib.lines.Line2D at 0xc0abef0>]

No Comment to " How to plot sine wave in Python? "