0

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.

Andrew
  • 1
  • 1
  • You can certainly plot x, y, and z, though as a 2D (two dimensional) plot you wind up having to flatten the x and matrixes, eg. scatter(x, y, z) . What should the plot look like? 2D or 3D? – Bill Jul 07 '23 at 10:40
  • It is just a 2D plot. The data along the x-axis has metadata that I would like to color elements by, and the data has associated groups. i.e. [(x1,y1),(x2,y2),(x3,y3)] belong to z1; [(x4,y4),(x5,y5),(x5,y5)] belong to z2. – Andrew Jul 07 '23 at 12:56
  • See example below. – Andrew Jul 07 '23 at 13:10
  • Still not clear. Do you want to have a scatter plot with individual color for each point? – Przemyslaw Szufel Jul 07 '23 at 15:22
  • Sorry, this would be for a scatter plot and z would be the color axis. [(x1,y1),(x2,y2),(x3,y3)] belong to z1 group and all points have the first color, [(x4,y4),(x5,y5),(x5,y5)] belong to z2 group and all points have the second color, etc. – Andrew Jul 07 '23 at 15:37

1 Answers1

0

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

  • Not quite, each z value is associated with one row. For example, if I set `z=rand(["A","B"],10)`, this does not work. If I swap N and M in your example, I see 10 keys in the legend. Does that make sense? – Andrew Jul 11 '23 at 18:11
  • I think I get what you mean; I'll update the answer. – Sharon Kartika Jul 11 '23 at 20:20