# Unable to update scatter plot via for loop

**URL:** <https://discourse.julialang.org/t/unable-to-update-scatter-plot-via-for-loop/93347>\
**Category:** New to Julia\
**Tags:** plots\
**Created:** [January 22, 2023, 5:17am UTC](https://discourse.julialang.org/t/unable-to-update-scatter-plot-via-for-loop/93347 "2023-01-22T05:17:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MK-1138](https://avatars.discourse-cdn.com/v4/letter/m/22d042/32.png) [@MK-1138](https://discourse.julialang.org/u/MK-1138)\
**Post date:** [January 22, 2023, 5:17am UTC](https://discourse.julialang.org/t/unable-to-update-scatter-plot-via-for-loop/93347/1 "2023-01-22T05:17:29Z")

</div>

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [January 22, 2023, 9:32am UTC](https://discourse.julialang.org/t/unable-to-update-scatter-plot-via-for-loop/93347/2 "2023-01-22T09:32:09Z")

</div>

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
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
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
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…

---

<div class="post-metadata">

**Author:** ![jmair](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmair/32/35117_2.png) [@jmair](https://discourse.julialang.org/u/jmair)\
**Post date:** [January 22, 2023, 10:10am UTC](https://discourse.julialang.org/t/unable-to-update-scatter-plot-via-for-loop/93347/3 "2023-01-22T10:10:27Z")

</div>

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

```julia
# 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.
