Search code examples
pythonplotsympycomplex-numbers

Plot an implicit equation with a complex variable in SymPy


I am trying to plot a SymPy complex number in python.

Let's say I want to plot the complex unit circle with the real part on the x-axis and the imaginary part on the y-axis. See my code:

from sympy import symbols, Eq
from sympy.plotting import plot_implicit

z = symbols('z')
expr = Eq(abs(z), 1)

p1 = plot_implicit(expr)

I am defining the unit circle through this equation: abs(z)=1. Then I am plotting the expression using plot_implicit. I get two vertical lines going through z=-1 and z=1 with z on the x-axis and f(z) on the y-axis.

How can I achieve a satisfactory plotting of the complex unit circle?


Solution

  • It'd be nice if plot_implicit could work directly with one complex symbol z, but at present it cannot: the expectation is that the user provides an expression with two real symbols. So do that, by introducing real x, y and making z an expression with x and y.

    from sympy import symbols, Eq, I
    from sympy.plotting import plot_implicit
    
    x, y = symbols('x y', real=True)
    z = x + I*y
    expr = Eq(abs(z), 1)    
    p1 = plot_implicit(expr)