Unable to update scatter plot via for loop

I have (x,y) coordinates pairs saved in two Excel woorksheets, within the same file (workbook).

I’m trying to plot a scatter plot by looping through

using XLSX
using Plots

# reading Excel file's worksheets specific ranges
coordX = XLSX.readdata("data.xlsx", "x", "B3:QU202")
coordY = XLSX.readdata("data.xlsx", "y", "B3:QU202")

# coordX and coordY are of equal size (200, 462)
(m, n) = size(coordX);

plot(0, 0, label="Key Plan")

for r in 1:m
    for c in 1:n
        plot!((coordX[r,c], coordY[r,c]), seriestype=:scatter)
    end
end

This doesn’t work and I do not understand why. I tried both VSCode and REPL/terminal. VSCode shows the Julia: Evaluating... line in the status bar for a while, then simply stops. REPL just hangs indefinitely (= for hours) until I interrupt it.

Ending the code with the plot(0, 0, label="Key Plan") line produces the empty plot with the label, but continuing from there I am unable to update this initial plot with the scatter points. coordX and coordY are 200x462 Matrix{Any}.

Can anyone tell me what I’m doing wrong? Thank you.

I don’t think this is surprising - you are creating a plot by re-drawing the whole thing over 90,000 times, and you are adding 90,000 individual series to the plot, each with its own legend. Consider:

julia> using Plots

julia> x = rand(10, 10); y = rand(10, 10);

julia> @time for i ∈ 1:size(x, 1)
           for j ∈ 1:size(x, 2)
               scatter!((x[i, j], y[i, j]))
           end
       end
  0.033949 seconds (26.72 k allocations: 2.104 MiB)

julia> x = rand(50, 50); y = rand(50, 50);

julia> @time for i ∈ 1:size(x, 1)
           for j ∈ 1:size(x, 2)
               scatter!((x[i, j], y[i, j]))
           end
       end
  6.499767 seconds (680.78 k allocations: 52.705 MiB, 0.38% gc time)

And here I “only” create 2,500 plots in the second example, so still ~20x less than you are trying to create. On the other hand:

julia> @time scatter(vec(x), vec(y)) # plot all in one series
  0.029125 seconds (12.00 k allocations: 670.451 KiB, 97.12% compilation time)

If you really do want to create one series per point that’s:

julia> @time scatter(vec(x)', vec(y)')
  2.295135 seconds (1.02 M allocations: 60.520 MiB, 1.00% gc time, 13.62% compilation time)

a lot slower, but I guess that’s expected as the plot object now has 2,500 series and a legend with 2,500 entries…

You should probably just convert and load the data and do a single call to scatter:

# Flatten and give types to arrays
x = [reshape(coordX, :)...]
y = [reshape(coordY, :)...]
scatter(x,y)

There’s probably a better way to do the type conversions, but this is just a quick and dirty way.