Search code examples
pythonpython-3.xnumpyplotcomplex-numbers

Why I am not able to plot a complex function (obtained with parsing) with matplotlib?


I am trying to plot a complex numbers function, but I have some problems (I don't usually use matplotlib, so may be a very stupid error). First of all, I've created a function to parse the real and complex part of a given mathematical function (n is a function index and x is the variable):

import parser

def e_parser( real_part, imaginary_part, n, x ): 
    real_p = parser.expr( real_part ).compile()
    imag_p = parser.expr( imaginary_part ).compile()
    
    return complex( eval( real_p ), eval( imag_p ) )

It works perfectly. Now I've created another function to plot the parsed complex mathematical function:

import numpy as np
import matplotlib.pyplot as plt

def plotter( real_part, imaginary_part, a, b, n ):   
    x = np.arange( a, b, ( ( a-b ) / 10 ) )
    
    def func( x ):
        return e_parser( real_part, imaginary_part, n, x )

    my_label = "Wave-function for n = " + str( n )
    
    plt.xlabel( "re" )
    plt.ylabel( "im" )
    plt.plot( np.real( func( x ) ), np.imag( func( x ) ), label = my_label )
    plt.legend()
    plt.show()

I tried this in a demo program:

import numpy as np
plotter( "np.sin(n*np.pi*x)", "x", 0, 1, 3 )

But the output is:

Traceback (most recent call last):
  File "main.py", line 130, in <module>
    main()
  File "main.py", line 21, in main
    ft.plotter( "np.sin(n*np.pi*x)", "x", 0, 1, 3 )
  File "/home/gianluca/WaveNCC/src/functions.py", line 208, in plotter
    plt.plot( np.real( func( x ) ), np.imag( func( x ) ), label = my_label )
  File "/home/gianluca/WaveNCC/src/functions.py", line 202, in func
    return ut.e_parser( real_part, imaginary_part, n, x )
  File "/home/gianluca/WaveNCC/src/utils.py", line 208, in e_parser
    return complex( eval( real_p ), eval( imag_p ) )
  File "<syntax-tree>", line 1, in <module>
TypeError: only size-1 arrays can be converted to Python scalars

If I try to plot the same function, but with the imaginary part set to 0:

Traceback (most recent call last):
  File "main.py", line 130, in <module>
    main()
  File "main.py", line 21, in main
    ft.plotter( "np.sin(n*np.pi*x)", 0, 0, 1, 3 )
  File "/home/gianluca/WaveNCC/src/functions.py", line 211, in plotter
    plt = cp.plot(
  File "/home/gianluca/.local/lib/python3.8/site-packages/cplot/_main.py", line 265, in plot
    extent = (x_range[0], x_range[1], y_range[0], y_range[1])
TypeError: 'int' object is not subscriptable

Do you know what is going on? Thanks.


Solution

  • complex can only take scalars as argument, not arrays. So you should use

    return eval( real_p ) +  eval( imag_p )  * 1j
    

    instead of

    return complex( eval( real_p ), eval( imag_p ) )
    

    (also, you certainly meant to write x = np.arange( a, b, ( ( b-a ) / 10 ) ) instead of x = np.arange( a, b, ( ( a-b ) / 10 ) ))