Subplot function in Julia

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

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?

& is bitwise and

julia> 0xF0 & 0x7f
0x70

You want logical and &&:

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

The expression you typed was parsed as

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.

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?

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.

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

“emdedding” what ?

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

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?

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

Screen Shot 2022-07-06 at 12.12.25 AM
I tried and still got nothing. Is it because I’m using Jupyter noterbook?

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)

1 Like

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.

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

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

what happens?

The plot shows correctly.

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

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

split(1, 2)

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

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:

import Plots
RM = rand(-1:0.1:3,2,122)
p = Plots.scatter(RM[1,:],RM[2,:])
S = split(1,2,RM)
q = Plots.scatter!(p,S[1,:],S[2,:])

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

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:

split_plot(1,2,RM)
1 Like