# Graph Piecewise Function in Julia

**URL:** <https://discourse.julialang.org/t/graph-piecewise-function-in-julia/48662>\
**Category:** Visualization\
**Tags:** plots\
**Created:** [October 19, 2020, 10:45pm UTC](https://discourse.julialang.org/t/graph-piecewise-function-in-julia/48662 "2020-10-19T22:45:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [October 19, 2020, 10:45pm UTC](https://discourse.julialang.org/t/graph-piecewise-function-in-julia/48662/1 "2020-10-19T22:45:13Z")

</div>

How do graph a piecewise function?

---

<div class="post-metadata">

**Author:** ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Post date:** [October 19, 2020, 10:58pm UTC](https://discourse.julialang.org/t/graph-piecewise-function-in-julia/48662/2 "2020-10-19T22:58:55Z")

</div>

The same way you plot any function:

```julia
using Plots

plot(1:50, sin)
f(x) = x<25 ? sin(x) : 0.5
plot(1:50, f)

```

The function f is defined piecewise:

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

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [October 19, 2020, 11:17pm UTC](https://discourse.julialang.org/t/graph-piecewise-function-in-julia/48662/3 "2020-10-19T23:17:23Z")

</div>

> [@Albert\_Zevelev](#):
>
> ```julia
> using Plots
> 
> plot(1:50, sin)
> f(x) = x<25 ? sin(x) : 0.5
> plot(1:50, f)
> 
> ```

great! where do I find this in the documentation? I want to do other things, like make x = 1/2 and otherwise x=2

---

<div class="post-metadata">

**Author:** ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Post date:** [October 19, 2020, 11:36pm UTC](https://discourse.julialang.org/t/graph-piecewise-function-in-julia/48662/4 "2020-10-19T23:36:56Z")

</div>

@brett_knoss here is how I approach this problem:  
Q: How do I graph a piecewise function?

Q1: how do I graph any function `f` in Julia?  
Google “Julia Plots”. Find this: [Basics · Plots](http://docs.juliaplots.org/latest/basics/)  
To plot any function `f(x)` over domain `dom`: `plot(dom, f)`  
Example `dom=1:50`, `f(x)=sin(x)`: `plot(1:50, sin)`

Q2: how do I create a piecewise function `f` in Julia?

```julia
function f(x)
    if x > 0 && x < 2 
        1
    elseif x >= 2 && x < 5
        2
    else
        7 
    end     
end

```

Then: `plot(-10:10,f)`

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [October 19, 2020, 11:41pm UTC](https://discourse.julialang.org/t/graph-piecewise-function-in-julia/48662/5 "2020-10-19T23:41:48Z")

</div>

thanks! that is handy. I found out in the mahematic opreators docs, that there is an unequal symbol !=

so I was able to put

```julia
f(x)=x!=0 ? .5 : 2

g(x)=x!=0 2 : .5

h(x)= f(x) *g(x)

plot(1:50,f)
plot!(1:50,g)

```

and all worked, except I can’t see my plots for some reason, also I don’t know how to select colors, but that shouldn’t matter.

---

<div class="post-metadata">

**Author:** ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Post date:** [October 19, 2020, 11:45pm UTC](https://discourse.julialang.org/t/graph-piecewise-function-in-julia/48662/6 "2020-10-19T23:45:58Z")

</div>

Works for me:

```julia
using Plots
f(x)=x!=0 ? .5 : 2
g(x)=x!=0 ? 2 : .5
h(x)= f(x)*g(x)
plot(1:50,f)
plot!(1:50,g)
plot!(1:50,h)

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [October 20, 2020, 7:41am UTC](https://discourse.julialang.org/t/graph-piecewise-function-in-julia/48662/7 "2020-10-20T07:41:35Z")

</div>

To answer your other question - you can set the `linecolor` attribute like so:

```julia
plot(1:50, f, linecolor = "mediumvioletred")

```
