2

If I want to plot something like y=x^2 then I can do something like

x = np.linspace(-10, 10, 1000)
plt.plot(x, x**2)

But how do I go about this if then equation is something like x + y + sin(x) + sin(y) = 0? I would rather not have to solve for y by hand. Is there some function that handles this?

theQman
  • 1,690
  • 6
  • 29
  • 52

2 Answers2

0

You can try contour plots:

from matplotlib.pyplot import *

def your_function(x, y):
    return 5 * np.sin(x) - 2 * np.cos(y)

x = np.linspace(-10, 10, 1000)
X, Y = np.meshgrid(x, x)
Z = your_function(X, Y)

CP = contour(X, Y, Z, [0])
grid()
show()
ForceBru
  • 43,482
  • 10
  • 63
  • 98
0

This will do the job:

import matplotlib.pyplot
import numpy as np
X, Y = np.meshgrid(np.arange(-10, 10, 0.05),np.arange(-10, 10, 0.05))
matplotlib.pyplot.contour(X, Y, X + Y + np.sin(X) + np.sin(Y), [0])
matplotlib.pyplot.show()
Miriam Farber
  • 18,986
  • 14
  • 61
  • 76
  • Hi Miriam, is there a way to extract the values of y from this plot? I have a function of a rotated cone that I can obtain the plot but I'm not sure how to obtain the values of Z. – Sean Nov 08 '22 at 22:07