Home > front end >  plot modulus functions in matplotlib
plot modulus functions in matplotlib

Time:11-12

im pretty new to matplotlib and plottings. im trying to plot the function |x| |y| = 0.1 in matplotlib. but im unable to do due to the below syntax. Is there a way i can do this with single function ? Also if the range of x and y values are increased beyond [-0.1,0.11] graph is not correct. Kindly help enter image description here

CodePudding user response:

You are plotting a function of two variables, hence you can use plt.contour. If you rewrite your equation as |x| |y| - 0.1, the "equal to zero" correspond to the contour level 0.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap

l = 0.25 # plot the function for x, y in [-0.25, 0.25]
n = 500j # number of discretization points
x, y = np.mgrid[-l:l:n, -l:l:n]

# create a colormap with a single color
cmap = ListedColormap(["tab:blue", "tab:blue"])
plt.figure()
plt.contour(x, y, abs(x)   abs(y) - 0.1, levels=[0], cmap=cmap)
plt.show()
  • Related