# Subplot function in Julia

**URL:** <https://discourse.julialang.org/t/subplot-function-in-julia/83810>\
**Category:** General Usage\
**Created:** [July 5, 2022, 11:48pm UTC](https://discourse.julialang.org/t/subplot-function-in-julia/83810 "2022-07-05T23:48:57Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![JosieG](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josieg/32/36221_2.png) [@JosieG](https://discourse.julialang.org/u/JosieG)\
**Post date:** [July 5, 2022, 11:48pm UTC](https://discourse.julialang.org/t/subplot-function-in-julia/83810/1 "2022-07-05T23:48:57Z")

</div>

Hi, I’m trying to get a plot function in Julia. This is my code

```julia
function split(pt1,pt2::Number)
    x = []
    y = []
    for i in 1:122
        if matrix[1,i] > pt1 & matrix[1,i] < pt2
            push!(x, matrix[1,i])
            push!(y, matrix[2,i])
        end
    a = transpose(hcat(x,y))
    Plots.scatter(a[1,:],a[2,:])
    end
end

```

But when I used it, like \>split(1,2), the error shows MethodError: no method matching &(::Int64, ::Float64). What’s wrong with this function?

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [July 6, 2022, 12:00am UTC](https://discourse.julialang.org/t/subplot-function-in-julia/83810/2 "2022-07-06T00:00:19Z")

</div>

`&` is bitwise and

```julia
julia> 0xF0 & 0x7f
0x70

```

You want logical and `&&`:

```julia
julia> false && false, false && true, true && false, true && true
(false, false, false, true)

```

The expression you typed was parsed as

```julia
julia> Meta.@lower 1.0 > 1 & 1.0 < 1.0
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─ %1 = 1 & 1.0
│ %2 = 1.0 > %1
└── goto #3 if not %2
2 ─ %4 = %1 < 1.0
└── return %4
3 ─ return false
))))

```

The error is reported because there is no defined method to compute the first expression, the bitwise and of an integer and a floating point number.

---

<div class="post-metadata">

**Author:** ![JosieG](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josieg/32/36221_2.png) [@JosieG](https://discourse.julialang.org/u/JosieG)\
**Post date:** [July 6, 2022, 12:15am UTC](https://discourse.julialang.org/t/subplot-function-in-julia/83810/3 "2022-07-06T00:15:42Z")

</div>

Thank you! I changed my function as

> #split the plot function  
> function split(pt1,pt2)  
> x =   
> y =   
> for i in 1:122  
> if embedding[1,i] \> pt1 && embedding[1,i] \< pt2  
> push!(x, embedding[1,i])  
> push!(y, embedding[2,i])  
> end  
> end  
> a = transpose(hcat(x,y))  
> plot = Plots.scatter(a[1,:],a[2,:])  
> return plot  
> end  
> But when I tried split(1,2) is no output there. What I want is a plot. Is there any way to modify the code?

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [July 6, 2022, 12:27am UTC](https://discourse.julialang.org/t/subplot-function-in-julia/83810/4 "2022-07-06T00:27:24Z")

</div>

No output or an empty plot?

If you get no output at all, it would be helpful to know more about your environment (Are you using vscode, terminal, notebook, etc?) and operating system. Does just `Plots.scatter(rand(3), rand(3))` produce output?

If it is an empty plot, then it is likely no points in `embedding` meet your condition.

---

<div class="post-metadata">

**Author:** ![JosieG](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josieg/32/36221_2.png) [@JosieG](https://discourse.julialang.org/u/JosieG)\
**Post date:** [July 6, 2022, 12:36am UTC](https://discourse.julialang.org/t/subplot-function-in-julia/83810/5 "2022-07-06T00:36:06Z")

</div>

There is no output at all. I’m using Jupyter Notebook. The Plots.scatter command can work well separately.  
 ![Screen Shot 2022-07-05 at 8.35.18 PM](https://global.discourse-cdn.com/julialang/original/3X/f/0/f0e9076e914bd9135e74ff8366b1f2f48a1a0b41.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:** [July 6, 2022, 2:57am UTC](https://discourse.julialang.org/t/subplot-function-in-julia/83810/6 "2022-07-06T02:57:47Z")

</div>

> [@JosieG](#):
>
> embedding

“emdedding” what ?

NB: try `display(Plots.scatter())`. Not a good idea to use reserved `plot` word as user variable.

---

<div class="post-metadata">

**Author:** ![JosieG](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josieg/32/36221_2.png) [@JosieG](https://discourse.julialang.org/u/JosieG)\
**Post date:** [July 6, 2022, 3:44am UTC](https://discourse.julialang.org/t/subplot-function-in-julia/83810/7 "2022-07-06T03:44:09Z")

</div>

embedding is the name of the matrix I used. I tried display(Plots.scatter(a[1,:],a[2,:])) but still got nothing. Is it impossible to get a plot output from a function?

---

<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:** [July 6, 2022, 3:55am UTC](https://discourse.julialang.org/t/subplot-function-in-julia/83810/8 "2022-07-06T03:55:26Z")

</div>

Try `display(plot(rand(10)))` inside your function instead, in order to check if something displays.

---

<div class="post-metadata">

**Author:** ![JosieG](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josieg/32/36221_2.png) [@JosieG](https://discourse.julialang.org/u/JosieG)\
**Post date:** [July 6, 2022, 4:13am UTC](https://discourse.julialang.org/t/subplot-function-in-julia/83810/9 "2022-07-06T04:13:27Z")

</div>

![Screen Shot 2022-07-06 at 12.12.25 AM](https://global.discourse-cdn.com/julialang/original/3X/8/5/85a078615849f5902d7ef8907e299835b96eca4c.png)  
I tried and still got nothing. Is it because I’m using Jupyter noterbook?

---

<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:** [July 6, 2022, 4:20am UTC](https://discourse.julialang.org/t/subplot-function-in-julia/83810/10 "2022-07-06T04:20:14Z")

</div>

Comment out the instruction: `# plot = Plots.scatter()` because you are overwriting the `plot` reserved word.

Set the last command of your function as: `return plot(rand(10))` and repeat your two last lines again: a = split(1,2); display(a)

---

<div class="post-metadata">

**Author:** ![JosieG](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josieg/32/36221_2.png) [@JosieG](https://discourse.julialang.org/u/JosieG)\
**Post date:** [July 6, 2022, 4:36am UTC](https://discourse.julialang.org/t/subplot-function-in-julia/83810/11 "2022-07-06T04:36:26Z")

</div>

I tried but still not working. I tried to erase the input parameter

> function split()  
> x =   
> y =   
> for i in 1:122  
> if embedding[1,i] \> 1 && embedding[1,i] \< 2  
> push!(x, embedding[1,i])  
> push!(y, embedding[2,i])  
> end  
> end  
> a = transpose(hcat(x,y))  
> #plot = Plots.scatter(a[1,:],a[2,:])  
> #return plot  
> display(Plots.scatter(a[1,:],a[2,:]))  
> #display(Plots.plot(rand(10)))  
> #return plot(rand(10))  
> end

and let pt1 = 1, pt2 = 2, the function can have a plot output now. But I still don’t know how to get a plot generator function with two input parameters.

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [July 6, 2022, 5:11am UTC](https://discourse.julialang.org/t/subplot-function-in-julia/83810/12 "2022-07-06T05:11:45Z")

</div>

> [@contradict](#):
>
> Does just `Plots.scatter(rand(3), rand(3))` produce output?

What was the result of this? i.e. If you put the following directly in a notebook cell, restart, then run:

```julia
import Plots
Plots.scatter(rand(10,2))

```

what happens?

---

<div class="post-metadata">

**Author:** ![JosieG](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josieg/32/36221_2.png) [@JosieG](https://discourse.julialang.org/u/JosieG)\
**Post date:** [July 6, 2022, 6:31pm UTC](https://discourse.julialang.org/t/subplot-function-in-julia/83810/13 "2022-07-06T18:31:58Z")

</div>

The plot shows correctly.

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [July 6, 2022, 9:49pm UTC](https://discourse.julialang.org/t/subplot-function-in-julia/83810/14 "2022-07-06T21:49:41Z")

</div>

Julia functions return the result of evaluating the last expression, so the explicit return statement is not needed here. Try this:

```julia
function split(pt1, pt2)
    x = []
    y = []
    for i in 1:122
        if embedding[1,i] > pt1 && embedding[1,i] < pt2
            push!(x, embedding[1,i])
            push!(y, embedding[2,i])
        end
    end
    a = transpose(hcat(x,y))
    Plots.scatter(a[1,:],a[2,:])
end

```

And then in a new cell

```julia
split(1, 2)

```

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [July 7, 2022, 2:17am UTC](https://discourse.julialang.org/t/subplot-function-in-julia/83810/15 "2022-07-07T02:17:12Z")

</div>

Great. Here’s how I would write your function:

```julia
function split(pt1::Number, pt2::Number, matrix::Matrix{T}) where {T}
   m, n = size(matrix)
   a = Matrix{T}(undef,m,0)
   for i in 1:n
       if pt1 < matrix[1,i] < pt2
          a = hcat(a, matrix[:,i])
       end
   end
   return a
end

```

so the `split` function is just returning the subset of columns.  
Then, in separate cells, I would test it like this:

```julia
import Plots

```

```julia
RM = rand(-1:0.1:3,2,122)

```

```julia
p = Plots.scatter(RM[1,:],RM[2,:])

```

```julia
S = split(1,2,RM)

```

```julia
q = Plots.scatter!(p,S[1,:],S[2,:])

```

So, doing the computation and plotting separately. If you want a function, you can do:

```julia
function split_plot(pt1::Number, pt2::Number, M::Matrix)
    p = Plots.scatter(M[1,:],M[2,:])
    S = split(pt1,pt2,M)
    q = Plots.scatter!(p, S[1,:], S[2,:])
    display(q) 
   return nothing
end

```

then test:

```julia
split_plot(1,2,RM)

```
