Matplotlib numpy math functions [closed]

I am new to matplotlib, I would like to know whether what I am doing is correct.

The function is sin^2(x-2)e^-x^2, where the limits of x and y are [0,2] and [0,2] respectively.

import matplotlib.pyplot as plt
import numpy as np

def f(x):
    return (np.sin(x-2)**2) *np.exp(np.power(-x,2))

x = np.linspace(-np.pi, np.pi, 100)

#f(x) = sin2(x-2)e^-x2
y = f(x)

fig = plt.figure(figsize=(7,5), dpi=100)
ax = plt.axes()
ax.plot(x, y, 'r', label='sin2(x-2)e^-x2')
ax.set(xlim=(0,2), ylim=(0,2),
      xlabel='x values', ylabel='y values', title='f(x) = sin2(x-2)e^-x2')

I don’t think my computation is correct. Is there anyway to improve this?


I think you have a problem with the parentheses in your function, leading you to square -x, instead of taking -x**2. Try this:

def f(x):
    return np.sin(x - 2)**2 * np.exp(-x**2)