julia Plots.jl: plot matrix with grouped rows

151 Views Asked by At

I have a set of data where

x: N×M Matrix{Float64} y: N×M Matrix{Float64} z: N-element Vector{String}

Where z is associated with the Nth row of the matrix. I am attempting to plot the data and group it by the z, but I'm not sure of the best way to go about this.

I've tried list comprehension:

[scatter!(x[i,:],y[i,:],group=z[:]) for i in size(y[:,1])]

But this gave me a bounds error, and was not working.

Are there any suggestions? I feel like this should be pretty simple, but I'm not well versed in julia.

1

There are 1 best solutions below

2
Sharon Kartika On

Did you mean something like this?

using Plots
N, M = 2, 10
x = rand(1:10, (N, M))
y = rand(1:10, (N, M))
z = ["A", "B"]
plot()
[scatter!(x[i, :], y[i, :], label=z[i]) for i in 1:length(z)]
plot!()

Output:

scatter plot with separate labels

Update:

There probably are built in functions to do this job, but this should be one way to do it.

using Plots
N, M = 6, 10
x = rand(1:10, (N, M))
y = rand(1:10, (N, M))
z = rand(["A", "B"], N)
plot()
[scatter!(vec(x[findall(z.==label), :]), vec(y[findall(z.==label), :]), label=label) for label in unique(z)]
plot!()

Logic: For each unique element in z, get the rows in x and y corresponding to it, convert to a vector, and plot.

Output: scatter plot