简体   繁体   中英

TypeError: Object of type 'Add' is not JSON serializable - Python Graph

I am working on trying to create a tangent approximation of a function. However, trying to find a way to graph it on top of the graph. Both functions work, but when I graph the functions, I come up with the type error "Object of type 'Add' is not JSON serializable"

x = sp.Symbol("x")
y = sp.Symbol("y")
f = 1*(np.e**(-2*(((x**2 + y**2)**(1/2))-1))-2*np.e**(-1*(((x**2 + y**2)**(1/2))-1)))
fx = f.diff(x)
fy = f.diff(y)
x = np.linspace(-5,5,50)
y = np.linspace(-5,5,50)
[xx,yy] = np.meshgrid(x,y)
ff = 1*(np.e**(-2*(((xx**2 + yy**2)**(1/2))-1))-2*np.e**(-1*(((xx**2 + yy**2)** 
(1/2))-1)))
uu = f.subs(x,2).subs(y,2) + fx.subs(x,2).subs(y,2)*(xx) + fy.subs(x,2).subs(y,2)*(yy)


import plotly.graph_objects as go
fig = go.Figure(data=[go.Surface(x=xx,y=yy,z=uu),go.Surface(x=xx,y=yy,z=ff)])
fig.show()

The error occurs on the last line of code.

The issue is related to your coefficients ( f.subs(x,2).subs(y,2) , fx.subs(x,2).subs(y,2) , and fy.subs(x,2).subs(y,2) ). The coefficients are of type <class 'sympy.core.numbers.Float'> , which is not compatible for computations with numpy arrays. You can convert the coefficients using the float() command.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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