# Mutating function plot in Makie, and plotting 2 data sets on the same figure

**URL:** https://discourse.julialang.org/t/mutating-function-plot-in-makie-and-plotting-2-data-sets-on-the-same-figure/103655
**Category:** New to Julia
**Tags:** plotting
**Created:** [September 8, 2023, 9:36am UTC](https://discourse.julialang.org/t/mutating-function-plot-in-makie-and-plotting-2-data-sets-on-the-same-figure/103655 "2023-09-08T09:36:36Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Axze-rgb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/axze-rgb/32/50986_2.png) [@Axze-rgb](https://discourse.julialang.org/u/Axze-rgb)
#### Post date: [September 8, 2023, 9:36am UTC](https://discourse.julialang.org/t/mutating-function-plot-in-makie-and-plotting-2-data-sets-on-the-same-figure/103655/1 "2023-09-08T09:36:36Z")

</div>

Hello,

I am trying to plot 2 histograms in the same figure using Makie,

> f, ax1, h1 = hist(df1[:, “AF”])  
> h2 = hist!(ax1, df2[:, “AF”])  
> f  
> but

> TypeError: non-boolean (Missing) used in boolean context

However, this codes work

> x = range(0, 10, length=100)
> 
> f2, ax, l1 = lines(x, sin)  
> l2 = lines!(ax, x, cos)  
> f2

I don’t understand why my code doesn’t work. I plot an histogram then I use the ! to tell it to add the second serie on the same figure. Why is it looking for a boolean?

Thank you

---

<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: [September 8, 2023, 9:41am UTC](https://discourse.julialang.org/t/mutating-function-plot-in-makie-and-plotting-2-data-sets-on-the-same-figure/103655/2 "2023-09-08T09:41:51Z")

</div>

This works fine for me:

```julia
julia> using CairoMakie

julia> f, ax1, h1 = hist(rand(100))

julia> hist!(ax1, 2*rand(100))
Combined{Makie.hist, Tuple{Vector{Float64}}}

julia> f

```

and shows the importance to create minimum working examples for errors that you want help with - what is `df1[:, "AF"]`? What is the actual stacktrace you are getting?

---

<div class="post-metadata">

### Author: ![Axze-rgb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/axze-rgb/32/50986_2.png) [@Axze-rgb](https://discourse.julialang.org/u/Axze-rgb)
#### Post date: [September 8, 2023, 9:46am UTC](https://discourse.julialang.org/t/mutating-function-plot-in-makie-and-plotting-2-data-sets-on-the-same-figure/103655/3 "2023-09-08T09:46:51Z")

</div>

Sorry I assumed it was a stupid syntax error on my part, here is the stack trace

> 1. **pick\_hist\_edges** (::Vector{Union{Missing, Float64}}, ::Int64)@_hist.jl:146_
> 2. **#map#136** @_scenes.jl:185_ [inlined]
> 3. **map** (::typeof(Makie.pick\_hist\_edges), ::MakieCore.Combined{Makie.hist, Tuple{Vector{Union{Missing, Float64}}}}, ::Observables.Observable{Vector{Union{Missing, Float64}}}, ::Observables.Observable{Any})@_scenes.jl:182_
> 4. **plot!** (::MakieCore.Combined{Makie.hist, Tuple{Vector{Union{Missing, Float64}}}})@_hist.jl:163_
> 5. **plot!** (::Makie.Scene, ::Type{MakieCore.Combined{Makie.hist, Tuple{Vector{Union{Missing, Float64}}}}}, ::MakieCore.Attributes, ::Tuple{Observables.Observable{Vector{Union{Missing, Float64}}}}, ::Observables.Observable{Tuple{Vector{Union{Missing, Float64}}}})@_interfaces.jl:398_
> 6. **var"#plot!#192"** (::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, ::typeof(MakieCore.plot!), ::Makie.Scene, ::Type{MakieCore.Combined{Makie.hist}}, ::MakieCore.Attributes, ::Vector{Union{Missing, Float64}})@_interfaces.jl:310_
> 7. **plot!** @_interfaces.jl:275_ [inlined]
> 8. **var"#plot#2000"** (::NamedTuple{(), Tuple{}}, ::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, ::typeof(MakieCore.plot), ::Type{MakieCore.Combined{Makie.hist}}, ::GridLayoutBase.GridPosition, ::Vector{Union{Missing, Float64}})@_figureplotting.jl:98_
> 9. **plot** (::Type{MakieCore.Combined{Makie.hist}}, ::GridLayoutBase.GridPosition, ::Vector{Union{Missing, Float64}})@_figureplotting.jl:72_
> 10. **var"#hist#879"** (::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, ::typeof(Makie.hist), ::GridLayoutBase.GridPosition, ::Vararg{Any})@_recipes.jl:34_
> 11. **hist** (::GridLayoutBase.GridPosition, ::Vararg{Any})@_recipes.jl:33_
> 12. **top-level scope** @_[Local: 6](http://localhost:1234/edit?id=f0d5e9d0-4e22-11ee-2a07-dbaf931c4c4f#)_

I am trying to plot 2 dataframeS, I imported them so

> df1=CSV.read(“/media/alessandro/Storage/MA/comparing\_GATK\_and\_bbtools/bbtools\_allele\_fraction.txt”, DataFrame; header = false)  
> rename!(df1,[:Column1] .=\> [:AF])

Here it what they look like

> 1  
> 1  
> 1  
> 0.063  
> 0.056  
> 0.056  
> 0.15  
> 0.25  
> 0.136  
> 0.136

roughly 2 million lines, datatype is Float64 which is correct.

hope this gives you all the info you asked for

EDIT:

I tried to copy your solution

> using CairoMakie  
> f, ax1, h1=hist(df1[:, “AF”])  
> hist!(ax1, df2[:, “AF”] )  
> Combined{Makie.hist, Tuple{Vector{Float64}}}  
> f

but this returns

> TypeError: non-boolean (Missing) used in boolean context
> 
> 1. **pick\_hist\_edges** (::Vector{Union{Missing, Float64}}, ::Int64)@_hist.jl:146_
> 2. **#map#136** @_scenes.jl:185_ [inlined]
> 3. **map** (::typeof(Makie.pick\_hist\_edges), ::MakieCore.Combined{Makie.hist, Tuple{Vector{Union{Missing, Float64}}}}, ::Observables.Observable{Vector{Union{Missing, Float64}}}, ::Observables.Observable{Any})@_scenes.jl:182_
> 4. **plot!** (::MakieCore.Combined{Makie.hist, Tuple{Vector{Union{Missing, Float64}}}})@_hist.jl:163_
> 5. **plot!** (::Makie.Scene, ::Type{MakieCore.Combined{Makie.hist, Tuple{Vector{Union{Missing, Float64}}}}}, ::MakieCore.Attributes, ::Tuple{Observables.Observable{Vector{Union{Missing, Float64}}}}, ::Observables.Observable{Tuple{Vector{Union{Missing, Float64}}}})@_interfaces.jl:398_
> 6. **var"#plot!#192"** (::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, ::typeof(MakieCore.plot!), ::Makie.Scene, ::Type{MakieCore.Combined{Makie.hist}}, ::MakieCore.Attributes, ::Vector{Union{Missing, Float64}})@_interfaces.jl:310_
> 7. **plot!** @_interfaces.jl:275_ [inlined]
> 8. **var"#plot!#1458"** (::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, ::typeof(MakieCore.plot!), ::Makie.Axis, ::Type{MakieCore.Combined{Makie.hist}}, ::MakieCore.Attributes, ::Vector{Union{Missing, Float64}})@_axis.jl:792_
> 9. **plot!** @_axis.jl:779_ [inlined]
> 10. **var"#plot!#1459"** (::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, ::typeof(MakieCore.plot!), ::Type{MakieCore.Combined{Makie.hist}}, ::Makie.Axis, ::Vector{Union{Missing, Float64}})@_axis.jl:809_
> 11. **plot!** (::Type{MakieCore.Combined{Makie.hist}}, ::Makie.Axis, ::Vector{Union{Missing, Float64}})@_axis.jl:807_
> 12. **var"#hist!#880"** (::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, ::typeof(Makie.hist!), ::Makie.Axis, ::Vararg{Any})@_recipes.jl:38_
> 13. **hist!** (::Makie.Axis, ::Vararg{Any})@_recipes.jl:37_
> 14. **top-level scope** @_[Local: 4](http://localhost:1234/edit?id=f0d5e9d0-4e22-11ee-2a07-dbaf931c4c4f#)_

Am I right to suspect there might be an unexpected value in my dataframes?

---

<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: [September 8, 2023, 10:03am UTC](https://discourse.julialang.org/t/mutating-function-plot-in-makie-and-plotting-2-data-sets-on-the-same-figure/103655/4 "2023-09-08T10:03:31Z")

</div>

Yes, you probably have missing values in `df2[:, "AF"]`:

```julia
julia> x = rand([missing; range(-1, 1, 100)], 10_000);

julia> f, ax1, h1 = hist(x); hist!(ax1, y); f
ERROR: TypeError: non-boolean (Missing) used in boolean context

```

---

<div class="post-metadata">

### Author: ![Axze-rgb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/axze-rgb/32/50986_2.png) [@Axze-rgb](https://discourse.julialang.org/u/Axze-rgb)
#### Post date: [September 8, 2023, 10:15am UTC](https://discourse.julialang.org/t/mutating-function-plot-in-makie-and-plotting-2-data-sets-on-the-same-figure/103655/5 "2023-09-08T10:15:37Z")

</div>

It’s probably the issue, any idea why the error says it’s a boolean problem? just out of curiosity

EDIT: your solution works like a charm now that I cleaned the input data. Though now I need to investigate why data are missing, but that’s not an issue with Julia

---

<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: [September 8, 2023, 11:14am UTC](https://discourse.julialang.org/t/mutating-function-plot-in-makie-and-plotting-2-data-sets-on-the-same-figure/103655/6 "2023-09-08T11:14:18Z")

</div>

You’d have to check the source code to see what is happening, but `missing` propagates through comparisons like `<` or `==` so presumably at some point the histogram wants to check whether a number is below the bin edge value to determine whether to add it to the count for that bin and something like this happens:

```julia
julia> if missing < bin_edge
           current_bin += 1
       end
ERROR: TypeError: non-boolean (Missing) used in boolean context

```

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [September 8, 2023, 11:46am UTC](https://discourse.julialang.org/t/mutating-function-plot-in-makie-and-plotting-2-data-sets-on-the-same-figure/103655/7 "2023-09-08T11:46:05Z")

</div>

This could use a better error message higher up the stack. Could you please open an issue?
