# How to plot a sympy.jl piecewise function?

**URL:** <https://discourse.julialang.org/t/how-to-plot-a-sympy-jl-piecewise-function/130310>\
**Category:** General Usage\
**Tags:** plots, sympy-plotting\
**Created:** [June 29, 2025, 5:56pm UTC](https://discourse.julialang.org/t/how-to-plot-a-sympy-jl-piecewise-function/130310 "2025-06-29T17:56:46Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [June 29, 2025, 5:56pm UTC](https://discourse.julialang.org/t/how-to-plot-a-sympy-jl-piecewise-function/130310/1 "2025-06-29T17:56:46Z")

</div>

Hello, good afternoon.  
Im trying to plot a piecewise function using sympy.jl and plots.jl withou success. May someone help please?

```julia
let
	@syms y C
	# Define os pontos do gráfico. Eles são necessários
	# para encontrar a área da forma de onda que iremos integrar.
	p1 = point(0, 0)
	p2 = point(1, 50)
	p3 = point(2, 0)
	p4 = point(3, -50)
	p5 = point(4, 0)

	@info lineexp
	# y - yn = m(x -xn). Devemos usar a função linear e calcular
	# o coeficiente angular(m), e o coeficiente linear (c).
	# O coeficiente angular é encontrado por Δy/Δx.
	# 0 < t < 1
	m1 = (p2.y - p1.y)/(p2.x - p1.x)
	l1 = first(solve(subs(lineexp, m=>m1, c=>0), y))
	# 1 < t < 3
	m2 = (p3.y - p2.y)/(p3.x - p2.x)
	l2 = first(solve(subs(y - p2.y ~ m*(t - p2.x), m=>m2), y))
	# 3 < t < 4
	m3 = (p5.y - p4.y)/(p5.x - p4.x)
	l3 = first(solve(subs(y - p4.y ~ m*(t - p4.x), m=>m3), y))
	# Olhe na figura de forma de onda do exemplo
	v = sympy.Piecewise(
		(l1, Gt(t, 0) & Gt(1, t)),
		(l2, Gt(t, 1) & Gt(3, t)),
		(l3, Gt(t, 3) & Gt(4, t))
	)

	@info v

	# Calcularemos a função de i
	fi = C*diff(v)
	@info fi
	iresult = simplify(subs(fi, C=>200e-6))
	@info iresult
	firesult = lambdify(iresult, t)
	time = LinRange(0, 4, 1000)
	ydata = firesult.(collect(time))
    plot(time, ydata)

end

```

```julia
MethodError: objects of type Float64 are not callable

The object of type `Float64` exists, but no method is defined for this combination of argument types when trying to treat it as a callable object.

Maybe you forgot to use an operator such as *, ^, %, / etc. ?

```

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [June 29, 2025, 6:00pm UTC](https://discourse.julialang.org/t/how-to-plot-a-sympy-jl-piecewise-function/130310/2 "2025-06-29T18:00:50Z")

</div>

Something like that in books:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/0/4036ff0e371c719c7ed5eb3a7984ca347430a349.png)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [June 29, 2025, 6:47pm UTC](https://discourse.julialang.org/t/how-to-plot-a-sympy-jl-piecewise-function/130310/3 "2025-06-29T18:47:56Z")

</div>

In Plots.jl’s `plot()` use the line type keyword: `lt=:step`, once you’ve sorted out your error.

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [June 29, 2025, 7:37pm UTC](https://discourse.julialang.org/t/how-to-plot-a-sympy-jl-piecewise-function/130310/4 "2025-06-29T19:37:08Z")

</div>

but the function is failing on line `ydata = firesult.(collect(time))`

```julia
MethodError: objects of type Float64 are not callable

The object of type `Float64` exists, but no method is defined for this combination of argument types when trying to treat it as a callable object.

Maybe you forgot to use an operator such as *, ^, %, / etc. ?

```

Cant understand as the data type is a function with a t parameter

```julia
Callable function with a single variable t

```

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [June 29, 2025, 7:49pm UTC](https://discourse.julialang.org/t/how-to-plot-a-sympy-jl-piecewise-function/130310/5 "2025-06-29T19:49:57Z")

</div>

> [@jcbritobr](#):
>
> ```julia-auto
> v = sympy.Piecewise(
> (l1, Gt(t, 0) & Gt(1, t)),
> (l2, Gt(t, 1) & Gt(3, t)),
> (l3, Gt(t, 3) & Gt(4, t))
> )
> 
> ```

Looks like some issue with `lambdify`. This should work for a plot:

```julia-auto
plot(t -> float(v(t)), a, b, lt = :step)

```

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [June 29, 2025, 7:55pm UTC](https://discourse.julialang.org/t/how-to-plot-a-sympy-jl-piecewise-function/130310/6 "2025-06-29T19:55:57Z")

</div>

what are a, and b variables? If they are x and y data, I dont have y, because firesult is not working.

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [June 29, 2025, 8:05pm UTC](https://discourse.julialang.org/t/how-to-plot-a-sympy-jl-piecewise-function/130310/7 "2025-06-29T20:05:36Z")

</div>

They are just numbers to pass to plot (0 and 4 in your case), as any other function would be plotted. (The issue here, is the lambdify call doesn’t work well with the last condition. If you put `true` instead of `Gt(t, 3) & Gt(4, t)` then the plot should happen as desired: `plot(v, 0, 4; lt=:step)`.

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [June 29, 2025, 9:18pm UTC](https://discourse.julialang.org/t/how-to-plot-a-sympy-jl-piecewise-function/130310/8 "2025-06-29T21:18:25Z")

</div>

If I put true, I will loose the range information. Is it not important to plot the derivative?

![image](https://global.discourse-cdn.com/julialang/original/3X/b/5/b523e7a54c2cc8827d0e6d1d389682f6f67080ff.png)

```julia
	v = sympy.Piecewise(
		(l1, true),
		(l2, true),
		(l3, true)
	)

	@info v

	# Calcularemos a função de i
	fi = C*diff(v)
	@info fi
	iresult = simplify(subs(fi, C=>200e-6))
	@info iresult
	firesult = lambdify(iresult)
	@info firesult
	time = LinRange(0, 4, 1000)
	plot(p -> float(v(p)), 0, 4, lt=:step)

```

Tryed it here, it plots, but its wrong:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/0/d0b0f6f259ba3b63208259317a3e5a7aeebe8eb9.png)

And with the diff function (firesult), are wrong also:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/1/b1f3020e8251559bc29d667e9130d6d79569baf7.png)

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [June 29, 2025, 9:37pm UTC](https://discourse.julialang.org/t/how-to-plot-a-sympy-jl-piecewise-function/130310/9 "2025-06-29T21:37:53Z")

</div>

I can create a conventional function with ifs based on this  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/5/b523e7a54c2cc8827d0e6d1d389682f6f67080ff.png)

But I’ll loose all facilities of sympy.

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [June 29, 2025, 9:44pm UTC](https://discourse.julialang.org/t/how-to-plot-a-sympy-jl-piecewise-function/130310/10 "2025-06-29T21:44:53Z")

</div>

Well, did with python

```python
y = symbols('y')
p1 = Point(0, 0)
p2 = Point(1, 50)
p3 = Point(2, 0)
p4 = Point(3, -50)
p5 = Point(4, 0)

display(linexp)

# Encontrar as partes das funções no gráfico
# y - yn = m(x - xn)
# 0 < t < 1
m1 = (p2.y - p1.y)/(p2.x - p1.x)
l1 = solve(linexp.subs({m: m1, c: 0}), y)[0]
# 1 < t < 3
m2 = (p3.y - p2.y)/(p3.x - p2.x)
l2 = solve(Eq(y - p2.y, m*(t - p2.x)).subs(m, m2), y)[0]
print(">>>")
print(m2) 
display(l2)
# 3 < t < 4
m3 = (p5.y - p4.y)/(p5.x - p4.x)
l3 = solve(Eq(y - p4.y, m*(t - p4.x)).subs(m, m3), y)[0] 
display(l1)
display(l2)
display(l3)

t_sym = symbols('t')
v = Piecewise(
    (l1, And(t_sym > 0, t_sym < 1)),
    (l2, And(1 < t_sym, t_sym < 3)),
    (l3, And(3 < t_sym, t_sym < 4)),
    (0, True)
)
display(v)

fi = C*diff(v)
iresult = sp.simplify(fi.subs(C, 200e-6))
display(iresult)
sp.plot(iresult)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/0/f03183de027ca99a789b5c329f1a9db9e263a75d.png)

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [June 30, 2025, 3:56pm UTC](https://discourse.julialang.org/t/how-to-plot-a-sympy-jl-piecewise-function/130310/11 "2025-06-30T15:56:54Z")

</div>

> [@jcbritobr](#):
>
> ```julia-auto
> (l1, Gt(t, 0) & Gt(1, t)),
> (l2, Gt(t, 1) & Gt(3, t)),
> (l3, Gt(t, 3) & Gt(4, t))
> 
> ```

Glad it worked out. For anyone playing along at home, this would also work within `SymPy.jl`:

```julia-auto
v = sympy.Piecewise(
       (l1, Gt(t, 0) & Gt(1, t)),                
       (l2, Gt(t, 1) & Gt(3, t)),
       (l3, Gt(t, 3) & Gt(4, t)),
       (0, true))
plot(v, -10, 10; le=:step)

```

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [July 1, 2025, 12:34am UTC](https://discourse.julialang.org/t/how-to-plot-a-sympy-jl-piecewise-function/130310/12 "2025-07-01T00:34:09Z")

</div>

Yep. It worked. This shows that the issue was the NAN in piecewise function.  
Thank you. When you add the condition (0, true), the result in the differentiated function was (0, otherwise).

 ![image](https://global.discourse-cdn.com/julialang/original/3X/c/5/c5b91230f0aebd3b32bcd5c59b69e87d98d39a04.png)
