-3

Suppose I have a function like the following:

a*x^2 + b*y^2 + c*x + d*y + e = 0

How can I plot the curve defined by this equation? For example for x^2 + y^2 - 1= 0, I expect to plot a circle.

I found no function to plot such equations. The equation which can be plotted is usually in the form of a polynomial function in respect of x:

y = a*x^n + ....

and the answers for the other question is:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

x = np.arange(-5, 5, 0.25)
y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(x, y)
F = 3 + 2*X + 4*X*Y + 5*X*X

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, F)
plt.show()

Which is a 2d surface in three dimensions, while I need a curve in 2d dimension.

Ahmad
  • 8,811
  • 11
  • 76
  • 141
  • 2
    What plotting library are you having problems with? Can you show your code? – timgeb Oct 30 '18 at 11:28
  • @Ahmad That's correct, it's kind of hard to understand without a problem statement. – timgeb Oct 30 '18 at 11:29
  • the same principles apply regardless of f(x) = 0 or f(x, y)=0. The only thing that changes is the number of dimensions. – Ma0 Oct 30 '18 at 11:31
  • @timgeb I described my problem further. There is no function or library for such thing. One could distinguish that by reading the formula. – Ahmad Oct 30 '18 at 11:32
  • @Ev.Kounis the number of dimension is the same `x` and `y`. – Ahmad Oct 30 '18 at 11:33
  • 1
    @timgeb I included the answers for the questions which are regarded as a duplicate. But they are for polynomial functions but not for the functions similar to mine. – Ahmad Oct 30 '18 at 11:38
  • 1
    The code block you have plots a `z(x,y)` surface, a 2d object. Your equation is `f(x,y)=0`, which gives you an implicit equation for a curve, a 1d object embedded in 1d. In particular, you have conical sections. You mention "surface" in your question. Please clarify the terminology to make it unambiguous what you want. – Andras Deak -- Слава Україні Oct 30 '18 at 11:39
  • @Ahmad thank you, that should help clear up the question – timgeb Oct 30 '18 at 11:40
  • @AndrasDeak sorry, I don't get you. There is only two variables `x` and `y` so it's a 2D surface and can be plotted. – Ahmad Oct 30 '18 at 11:48
  • 1
    @Ahmad `f(x,y) = x + y` has two variables and can be plotted as a 2d surface. `f(x,y) = x + y = 0` is an equation defining a _level set_ of this function, which is the 1d curve given by `y = -x` in the `(x,y)` plane. Same thing for your case. Do you want to plot `f(x,y)` as a surface, or do you want the `f(x,y)=0` curves? – Andras Deak -- Слава Україні Oct 30 '18 at 11:50
  • 1
    @AndrasDeak the latter `f(x,y) = 0` in a 2D plane (x vs y). – Ahmad Oct 30 '18 at 11:52
  • @AndrasDeak for example `x^2 + y^2 -1= 0` is a circle. – Ahmad Oct 30 '18 at 11:55

1 Answers1

2

What you need is the level set of a 2d surface corresponding to the 0 value. You should use pyplot.contour to offload the hard part of the work. You only need to define the range you're interested in and the function over a grid. Modifying the example code in your question:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-5, 5, 50)
y = np.linspace(-5, 5, 50)
X, Y = np.meshgrid(x, y)
F = 5*X**2 - 4*X*Y + 2*Y**2 + 3*X - 4*Y - 1

fig,ax = plt.subplots()
ax.contour(X, Y, F, levels=[0]) # take level set corresponding to 0
plt.show()

result

The finer the mesh, the smoother the contour line.