Skip to content
Advertisement

sympy matplolib piecewise TypeError?

matplotlib TypeError? Please tell me the difference.

i want to use plt.plot

OK

JavaScript

Error.why?

JavaScript

raise TypeError(“can’t convert expression to float”) TypeError: can’t convert expression to float

(ref)

Sympy issues with plotting a piecewise function

sympy piecewise:How can I plot a piecewise function using matplotlib?

(20220315)

enter image description here

JavaScript

Advertisement

Answer

Matplotlib expects arrays of points rather than symbolic expressions. SymPy’s plot function is designed to handle that conversion for you. If you want to use matplotlib’s plot function then you need to make arrays of x and y values rather than hand it an expression. The efficient way to do that is with lambdify:

JavaScript

https://docs.sympy.org/latest/modules/utilities/lambdify.html#sympy.utilities.lambdify.lambdify

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement