简体   繁体   English

为什么我不能用 matplotlib 对复杂的 function(通过解析获得)进行 plot?

[英]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).我正在尝试 plot 一个复数 function,但是我有一些问题(我通常不使用 matplotlib,所以可能是一个非常愚蠢的错误)。 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):首先,我创建了一个 function 来解析给定数学 function 的实数和复数部分(n 是 function 索引和 x:

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:现在我创建了另一个 function 到 plot 解析的复杂数学 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:但是 output 是:

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:如果我尝试 plot 相同的 function,但虚部设置为 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.谢谢。

complex can only take scalars as argument, not arrays. complex只能将标量作为参数,而不是 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, ( ( ba ) / 10 ) ) instead of x = np.arange( a, b, ( ( ab ) / 10 ) ) ) (另外,您当然要写x = np.arange( a, b, ( ( ba ) / 10 ) )而不是x = np.arange( a, b, ( ( ab ) / 10 ) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM