Home > front end >  'Add' object is not callable
'Add' object is not callable

Time:11-23

I want to make a graphic with matplotlib and I have a problem with the class Intern Operation. This is the function:

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()

The imports are:

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

The traceback:

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

CodePudding user response:

That happens because your f is a symbolic expression, not a numerical function. So, inside your Halley.py file you would have to use sympy's lambdify to convert self.equation to a numerical function:

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