# Divide plot into boxes

I use this code to distribute points over a grid in the first photo

``````Npopulations = 10000
Person = [Dict("x" => 0,"y" => 0,"color" => "white","timecounter" => 0)  for i  ∈ 1:Npopulations]
yincrement = 0
for ipop in 2:Npopulations
Person[ipop]["x"] = Person[ipop-1]["x"] + gapstep
Person[ipop]["y"] += yincrement
if (Person[ipop]["x"] + gapstep > Lgrid)
Person[ipop]["x"] = 0
yincrement += gapstep
Person[ipop]["y"] = yincrement
end
end
`````` My question is how to distribute them like this hand written photo with lines between or in boxes ?

1 Like

There are probably more elegant approachers, but this should get you started. You will need to replace the random values with your own values.

``````using Plots, Random
Random.seed!(5474)

n_lines = 2
min_x = 0
max_x = 10
min_y = 0
max_y = 10
mid_point = (2 + n_lines) / 2 + 1

xvals = range(min_x, max_x, length = n_lines + 2)
yvals = range(min_y, max_y, length = n_lines + 2)

p = plot(framestyle=:box, grid=false,
xlims=(min_x,max_x), ylims=(min_y,max_y))
for r in 2:length(xvals), c in 2:length(yvals)
rnd_x = rand(5) * (xvals[c] .- xvals[c-1] .- 2 * padding) .+ xvals[c-1] .+ padding
rnd_y = rand(5) * (yvals[r] .- yvals[r-1] .- 2 * padding) .+ yvals[r-1] .+ padding
color = (r == mid_point) && (c == mid_point) ? :black : :red
scatter!(rnd_x, rnd_y, leg=false, markerstrokecolor=color,
markercolor=:white, markerstrokewidth=4)
c ≠ length(xvals) ? hline!([xvals[c]], color=:red) : nothing
r ≠ length(xvals) ? vline!([yvals[r]], color=:red) : nothing
end
display(p)
savefig("example.png")
`````` 2 Likes