plot the function 𝑓(𝑥,𝑦)=𝑠𝑖𝑛(𝑥)∗𝑦 in the range from 𝑥=[0,2𝜋] and 𝑦=[0,1] in python

To plot the function 𝑓(𝑥,𝑦)=𝑠𝑖𝑛(𝑥)∗𝑦 in the given range using python, we can follow the code below:

main.py
import matplotlib.pyplot as plt
import numpy as np

# defining the function
def f(x,y):
    return np.sin(x)*y

# creating the meshgrid for x and y
x = np.linspace(0, 2*np.pi, 100)
y = np.linspace(0, 1, 50)
X, Y = np.meshgrid(x, y)

# evaluating the function for each point in the meshgrid
Z = f(X, Y)

# plotting the function
fig, ax = plt.subplots()
ax.contourf(X,Y,Z, cmap='viridis')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title('f(x,y) = sin(x)*y')
plt.show()
470 chars
23 lines

The np.linspace() function is used to create linearly spaced arrays for x and y. The np.meshgrid() function is then used to create a grid of x and y values for which we can evaluate the function. Finally, we plot the function using plt.contourf(), which creates a filled contour plot. The result should be a 2D plot of the function f(x,y) over the given range.

related categories

gistlibby LogSnag