numpy "Add"对象不可调用

5lhxktic  于 2022-11-23  发布在  其他
关注(0)|答案(1)|浏览(172)

我想用matplotlib做一个图形,但我在使用类Intern Operation时遇到了一个问题。函数如下:

def makeGraphic(self, f, root):
        plt.rcParams['axes.unicode_minus'] = False

        fig = plt.figure(figsize=(6, 4))  # Nuevo lienzo
        # Use el método axisartist.Subplot para crear un objeto de área de dibujo ax
        ax = axisartist.Subplot(fig, 111)
        fig.add_axes(ax)

        X = np.linspace(-5, 10, 100)  

        Y = [f(x) for x in X]  # here I have the problem

        ax.plot(X, Y)  
        ax.scatter(root, 0, color='red')
        #plt.legend([r'$a>1$'], loc ='lower right') 
        print(max(X), max(Y))  
        ax.axis[:].set_visible(False) 
        ax.axis["x"] = ax.new_floating_axis(0, 0, axis_direction="bottom")  
        ax.axis["y"] = ax.new_floating_axis(1, 0, axis_direction="bottom") 
        ax.axis["x"].set_axisline_style("-|>", size=1.0) 
        ax.axis["y"].set_axisline_style("-|>", size=1.0) 

        ax.annotate('x', xy=(5, 0), xytext=(4+1, 0.3))  
        ax.annotate('y', xy=(0, 1.0), xytext=(-0.2, 5))  

        plt.xlim(-5, 5)  
        plt.ylim(-5, 5)  
        X_lim = np.arange(-4, 4+1, 1)
        ax.set_xticks(X_lim)  
        Y_lim = np.arange(-10,10+1, 1)
        ax.set_yticks(Y_lim) 

        fstr = str(f)

        ax.annotate(rf'$y = {fstr}$', xy=(8, -3), xytext=(8, -3))
        #plt.legend()
        plt.show()

导入包括:

import numpy as np
from sympy import diff

import matplotlib.pyplot as plt
import mpl_toolkits.axisartist as axisartist
#CONSTANTS
from operaciones.utils.constants import x

回溯:

Traceback (most recent call last):
  File "c:\Users\user\Desktop\MetodosNumericConsola\main.py", line 13, in <module>
    hal.pointsToGraphic()
  File "c:\Users\user\Desktop\MetodosNumericConsola\operaciones\First\Halley.py", line 49, in pointsToGraphic
    super().makeGraphic(self.equation, self.root)
  File "c:\Users\emily\Desktop\MetodosNumericConsola\operaciones\ListOp\InternOperation.py", line 49, in makeGraphic
    Y = [f(x) for x in X]  
        ^^^^^^^^^^^^^^^^^
  File "c:\Users\emily\Desktop\MetodosNumericConsola\operaciones\ListOp\InternOperation.py", line 49, in <listcomp>
    Y = [f(x) for x in X]  
         ^^^^
TypeError: 'Add' object is not callable
bhmjp9jg

bhmjp9jg1#

这是因为f是一个符号表达式,而不是一个数值函数,所以,在Halley.py文件中,你必须使用sympy的lambdifyself.equation转换为一个数值函数:

# hopefully your expression has one symbol...
f = lambdify(list(self.equation.free_symbols), self.equation)
super().makeGraphic(f, self.root)

相关问题