using for loop for plotting formula in Python | applied electronics engineering

Latest

using for loop for plotting formula in Python

By Applied Electronics - Tuesday, July 19, 2016 No Comments
Python programming language is ideal for doing scientific and engineering calculation and plotting graph of functions. Here is how one can get the y-axis values for linearly spaced x values.

import matplotlib.pyplot as plt
from math import exp

y = []
x = []

for u in range(-10,10):
    v = exp(u)
    y.append(v)
    x.append(u)
   
plt.plot(x, y)
plt.show

In the python code above, we have used the exponential function in the y axis. Using the for loop we have defined the range of the x values and for each x value we computed the exponential value y.

Here is the plot:



The range function is not particularly useful because it accepts integer as the stepping value and this hinders to use smaller x values.

No Comment to " using for loop for plotting formula in Python "