I want to plot a curve which is actually has a S shape:
- draw a circle
- move the down part of half circle a whole diameter distance
but the code says TypeError: can't convert complex to float
. Where is the complex number? how to fix it? Thanks
import sympy as sp
from sympy.plotting import *
u = sp.Symbol('u', real=True)
R0 = 2
boundLower = 0
boundUpper = 2 * sp.pi
x_u = sp.Piecewise(
(R0*sp.cos(u)+R0, sp.And(boundLower <= u, u <= boundUpper/2)),
(R0*sp.cos(u)+3*R0, sp.And(boundUpper/2 < u, u <= boundUpper)),
)
y_u = sp.Piecewise(
(R0*sp.sin(u), sp.And(boundLower <= u, u <= boundUpper/2)),
(R0*sp.sin(u), sp.And(boundUpper/2 < u, u <= boundUpper)),
)
plot_parametric(x_u, y_u, (u, boundLower, boundUpper))
SymPy's
plot
involves some error-prone manipulations meant to boost the performance of plots; they involvecomplex
data type which sometimes leads to errors when inequalities are involved. Reducing the number of inequalities helps in this case:The above is how Piecewise is usually presented in SymPy: conditions are evaluated in the order given, and the first one to be True results in the evaluation of the corresponding expression. (
u <= boundUpper
could be replaced byTrue
, by the way.) Result:This is still not ideal. The horizontal line from 0 to 4 should not be there, of course - it's an artifact of plotting. Also, this code displays
I suggest to avoid Piecewise in plotting. Instead combine a plot from pieces using
extend
as shown below.Output (no warnings and no artifacts).