# Plotting Function With if-else Using Gadfly.jl

**URL:** <https://discourse.julialang.org/t/plotting-function-with-if-else-using-gadfly-jl/58020>\
**Category:** Visualization\
**Tags:** gadfly\
**Created:** [March 26, 2021, 2:43pm UTC](https://discourse.julialang.org/t/plotting-function-with-if-else-using-gadfly-jl/58020 "2021-03-26T14:43:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adam\_Fleischhacker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adam_fleischhacker/32/24468_2.png) [@Adam\_Fleischhacker](https://discourse.julialang.org/u/Adam_Fleischhacker)\
**Post date:** [March 26, 2021, 2:43pm UTC](https://discourse.julialang.org/t/plotting-function-with-if-else-using-gadfly-jl/58020/1 "2021-03-26T14:43:15Z")

</div>

I am trying to define and plot a density function. However, it seems the if-else logic of my function is not supported for this kind of plot. Am I doing something wrong?

```julia
## graph density of spinner
using Gadfly

function probDens(x::Real)
    if x >= 0 && x <=1
        6*x*(1-x)
    else
        x
    end
end

# plot(f::Function, lower, upper, elements...; mapping...)
plot(probDens,0,1) ## works as expected
plot(probDens,-1,1) ## gives error

```

---

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [March 26, 2021, 2:52pm UTC](https://discourse.julialang.org/t/plotting-function-with-if-else-using-gadfly-jl/58020/2 "2021-03-26T14:52:59Z")

</div>

This worked for me on Gadfly 1.3.2. Please include the error message and Gadfly version.

---

<div class="post-metadata">

**Author:** ![Adam\_Fleischhacker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adam_fleischhacker/32/24468_2.png) [@Adam\_Fleischhacker](https://discourse.julialang.org/u/Adam_Fleischhacker)\
**Post date:** [March 26, 2021, 3:02pm UTC](https://discourse.julialang.org/t/plotting-function-with-if-else-using-gadfly-jl/58020/3 "2021-03-26T15:02:29Z")

</div>

Thanks for the help. I am also on 1.3.2. Here is the error:

```julia
julia> Pkg.status("Gadfly")
Status `C:\Users\ajf\OneDrive\toolingForDS\Project.toml`
  [c91e804a] Gadfly v1.3.2

julia> plot(π_X,-1,1) ## gives error
Error showing value of type Plot:
ERROR: MethodError: Cannot `convert` an object of type Nothing to an object of type Float64
Closest candidates are:
  convert(::Type{T}, ::Ratios.SimpleRatio{S}) where {T<:AbstractFloat, S} at C:\Users\ajf\.julia\packages\Ratios\uRs4y\src\Ratios.jl:13
  convert(::Type{T}, ::T) where T<:Number at number.jl:6
  convert(::Type{T}, ::Number) where T<:Number at number.jl:7
  ...
Stacktrace:
 [1] setindex!(::Array{Float64,1}, ::Nothing, ::Int64) at .\array.jl:847
 [2] apply_statistic(::Gadfly.Stat.FunctionStatistic, ::Dict{Symbol,Gadfly.ScaleElement}, ::Gadfly.Coord.Cartesian, ::Gadfly.Aesthetics) at C:\Users\ajf\.julia\packages\Gadfly\nN3lf\src\statistics.jl:1446
 [3] apply_statistics(::Array{Gadfly.StatisticElement,1}, ::Dict{Symbol,Gadfly.ScaleElement}, ::Gadfly.Coord.Cartesian, ::Gadfly.Aesthetics) at C:\Users\ajf\.julia\packages\Gadfly\nN3lf\src\statistics.jl:33
 [4] render_prepare(::Plot) at C:\Users\ajf\.julia\packages\Gadfly\nN3lf\src\Gadfly.jl:680
 [5] render(::Plot) at C:\Users\ajf\.julia\packages\Gadfly\nN3lf\src\Gadfly.jl:740
 [6] draw at C:\Users\ajf\.julia\packages\Gadfly\nN3lf\src\Gadfly.jl:847 [inlined]
 [7] show(::Base.GenericIOBuffer{Array{UInt8,1}}, ::MIME{Symbol("image/svg+xml")}, ::Plot) at C:\Users\ajf\.julia\packages\Gadfly\nN3lf\src\Gadfly.jl:945
 [8] display(::VSCodeServer.InlineDisplay, ::MIME{Symbol("image/svg+xml")}, ::Plot) at c:\Users\ajf\.vscode\extensions\julialang.language-julia-1.1.37\scripts\packages\VSCodeServer\src\display.jl:0
 [9] display(::VSCodeServer.InlineDisplay, ::String, ::Any) at .\multimedia.jl:216
 [10] display(::VSCodeServer.InlineDisplay, ::Plot) at c:\Users\ajf\.vscode\extensions\julialang.language-julia-1.1.37\scripts\packages\VSCodeServer\src\display.jl:108
 [11] display(::Any) at .\multimedia.jl:328
 [12] #invokelatest#1 at .\essentials.jl:710 [inlined]
 [13] invokelatest at .\essentials.jl:709 [inlined]
 [14] print_response(::IO, ::Any, ::Bool, ::Bool, ::Any) at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\REPL\src\REPL.jl:238
 [15] print_response(::REPL.AbstractREPL, ::Any, ::Bool, ::Bool) at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\REPL\src\REPL.jl:223
 [16] (::REPL.var"#do_respond#54"{Bool,Bool,REPL.var"#64#73"{REPL.LineEditREPL,REPL.REPLHistoryProvider},REPL.LineEditREPL,REPL.LineEdit.Prompt})(::Any, ::Any, ::Any) at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\REPL\src\REPL.jl:822
 [17] #invokelatest#1 at .\essentials.jl:710 [inlined]
 [18] invokelatest at .\essentials.jl:709 [inlined]
 [19] run_interface(::REPL.Terminals.TextTerminal, ::REPL.LineEdit.ModalInterface, ::REPL.LineEdit.MIState) at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\REPL\src\LineEdit.jl:2355
 [20] run_frontend(::REPL.LineEditREPL, ::REPL.REPLBackendRef) at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\REPL\src\REPL.jl:1144
 [21] (::REPL.var"#38#42"{REPL.LineEditREPL,REPL.REPLBackendRef})() at .\task.jl:356

```

If I experiment with a pdf from the Distributions.jl package, things seem to work.

```julia
using Distributions
densFun = function(x::Real)
    pdf(Beta(2,4),x)
end
plot(densFun,-1,2) ## this works

```

---

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [March 26, 2021, 3:10pm UTC](https://discourse.julialang.org/t/plotting-function-with-if-else-using-gadfly-jl/58020/4 "2021-03-26T15:10:11Z")

</div>

What is your function `π_X`?

---

<div class="post-metadata">

**Author:** ![Adam\_Fleischhacker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adam_fleischhacker/32/24468_2.png) [@Adam\_Fleischhacker](https://discourse.julialang.org/u/Adam_Fleischhacker)\
**Post date:** [March 26, 2021, 3:12pm UTC](https://discourse.julialang.org/t/plotting-function-with-if-else-using-gadfly-jl/58020/5 "2021-03-26T15:12:26Z")

</div>

You are on the problem. When I name my function like this:

```julia
function π_X(x::Real)
    if x >= 0 && x <=1
        6*x*(1-x)
    else
        x
    end
end

```

I get the error. If I rename the function `probDens`, then it works. I renamed the function for the forum as to not seem too fancy. Lo and behold, it is the function name that is the issue. Do you know why?

---

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [March 26, 2021, 3:14pm UTC](https://discourse.julialang.org/t/plotting-function-with-if-else-using-gadfly-jl/58020/6 "2021-03-26T15:14:50Z")

</div>

You function `π_X` also works for me. I can’t reproduce the error in your post above.

---

<div class="post-metadata">

**Author:** ![Adam\_Fleischhacker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adam_fleischhacker/32/24468_2.png) [@Adam\_Fleischhacker](https://discourse.julialang.org/u/Adam_Fleischhacker)\
**Post date:** [March 26, 2021, 3:21pm UTC](https://discourse.julialang.org/t/plotting-function-with-if-else-using-gadfly-jl/58020/7 "2021-03-26T15:21:20Z")

</div>

Thanks for working with me on this. I restarted my Julia session and the bug goes away. I am pretty sure multiple dispatch is to blame. I originally typed the function argument `x::Float64` and then switched to `x::Real`. I guess the incomplete function I wrote for `Float64` took precedence over the complete one I wrote for `x::Real`.

Lessons Learned:

1. Restart session and reproduce bug before posting
2. Consider Revise.jl?

If there are other lessons to be learned … especially about using types, please share. Thanks 🙂

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [March 26, 2021, 5:19pm UTC](https://discourse.julialang.org/t/plotting-function-with-if-else-using-gadfly-jl/58020/8 "2021-03-26T17:19:55Z")

</div>

I usually type function args only for dispatch purpose (or for clarity for exported functions in a public API).

Revise.jl is a must !

6x in place of 6\*x and your function could nicely be written in one line

```julia
 π_Xb(x) = (x >= 0 && x <=1) ? 6x*(1-x) : x

```

Depending on the x distribution (if a branch is more probable and in this case which branch) this can be faster 😉

```julia
 π_Xc(x) = (x < 0 || x >1) ? x : 6x*(1-x)

```
