It is better to use linspace than creating a list of linearly spaced numbers for plotting the x-axis scale points. linspace( ) is a numpy function so you have to import the numpy first.

linspace( ) function can be used in the following way. Suppose we want to plot sine function, exp( ) etc or any function really.

We will make a sine wave plot for this illustration. First we define the time axis from 0 to 2pi and plot the sine wave in this range.

[In] import numpy as np

[In] import matplotlib.pyplot as plt

[In] t = np.linspace(0,2*np.pi,100)

[In] y = np.sin(t)

[In] plt.plot(t,y)

[Out] [<matplotlib.lines.Line2D at 0xc207f10>]

 Following is example of exponential function.

[In] z = np.exp(t)

 [In] plt.plot(t,z)

[Out] [<matplotlib.lines.Line2D at 0xc007130>]

See also other ways to plot sine wave in python.


Post a Comment