Grid creation and donut-like display and color filling

Hello,

Do you know how I could create a grid like this one on Pluto? In this grid, I would like to put numbers in it (not in all the boxes) and draw curves over it.

Have a nice day

What does “in Pluto” mean here? Could you not just use a normal plot which has gridlines?

julia> using Plots

julia> plot(xticks = 1:20, yticks = 1:20, xlim = (1, 20), ylim = (1,20), aspect_ratio = 1)

image

2 Likes

By that I mean “using a Pluto notebook”.
Thank you ! And is it possible to “draw” over it as well as include numbers in the boxes?

Sure, you can do

julia> annotate!([3], [3], text("3"))

to put the number “3” on the location (3,3) in the above plot

3 Likes

Another way:

using Plots; gr()

# Define grid
Nx, Ny = 12, 6    # number of grid cells
x1, x2 = 0, 240   # xgrid limits
y1, y2 = 0, 120   # ygrid limits
x, y = LinRange(x1,x2,Nx+1), LinRange(y1,y2,Ny+1)
dx, dy = (x2-x1)/Nx, (y2-y1)/Ny

# Plot grid
p = plot(legend=false,xlim=(x1,x2),ylim=(y1,y2),ratio=1,grid=false)
xi, yi = x1 .+ zeros(Nx+1), y1 .+ zeros(Ny+1)
[ plot!(p, fill(x[i],Ny+1), y, lc=:gray, lw=0.2) for i in 1:Nx+1 ];
[ plot!(p, x, fill(y[i],Nx+1), lc=:gray, lw=0.2) for i in 1:Ny+1 ];

# Annotate np points with coords (xr,yr) and draw connecting lines:
np = 7
xr, yr = x[rand(1:Nx,np)] .+ dx/2, y[rand(1:Ny,np)] .+ dy/2
labels = string.(1:np)
plot!(xr,yr, marker=2.)
[annotate!(xr[i], yr[i] + (y2-y1)/20, text(labels[i],7)) for i in 1:np];
plot(p, title="Grid plot", axis=nothing, framestyle=:none)

Grid_plot_with_labels_and_lines1

Or showing the axes:

plot(p, title="Grid plot", xticks=x1:dx*2:x2, yticks=y1:dy*2:y2, framestyle=:box)

Grid_plot_with_labels_and_lines2

2 Likes

And with GMT
(could have used the automatic region detection but would risk to trim out some text)

# Number points with the record number
xy = rand(7,2);
plot(xy, region=(0., 1.1, 0, 1.1), frame=(axes=:WSen, annot=0.2, grid=0.2), marker=:point, lc=:green)
text!(xy, offset=(shift=(0,0.5),), rec_number=true, name="um.png", show=true)

# Use any label we want
Dxy = mat2ds(rand(7,2), ["A1","A2","A3","A4","A5","A6","A7"]);
plot(Dxy, region=(0., 1.1, 0, 1.1), frame=(axes=:WSen, annot=0.2, grid=0.2), marker=:point, lc=:green)
text!(Dxy, offset=(shift=(0,0.5),), name="dois.png", show=true)

1 Like

Many thanks to you gentlemen!
Could I benefit from your experience?
I wonder if it is possible to draw a random curve that would close. Added to this curve would be another curve that would follow approximately the same path and would be located at a certain distance from the other curve. And also “black out” the squares that are not between the two curves.

@Stephen1, a simple way, but not ideal, is to use plot shapes (and then to plot grid defined in previous post over the shapes).

using Plots; gr()

function Shape1(C,r)
    θ = LinRange(0,2π, 72)
    C[1] .+ r*(1 .+ 0.2*sin.(4θ)).*sin.(θ),  C[2] .+ r*(1 .+ 0.2*sin.(4θ)).*cos.(θ)
end

C = [120,60]; r = 40;
S1 = Shape1(C,r)
S2 = (C[1] .+ (S1[1] .- C[1])*0.7, C[2] .+ (S1[2] .- C[2])*0.7)

p = plot(legend=false,xlim=(x1,x2),ylim=(y1,y2),ratio=1,grid=false)
plot!(S1, seriestype=[:shape],lw =0.5,c=:blue,lc=:black,legend=false,fa=0.2,ratio=1)
plot!(S2, seriestype=[:shape],lw =0.5,c=:white,lc=:black,legend=false,fa=1,ratio=1)

Four-leaf_clover_donut

3 Likes

or using Luxor.jl
(can probably be simplified, I am a Luxor Newbie)

using Luxor
function drawGrid(X,Y)
	for i=-X:50:X
		line(Point(i,-Y),Point(i,Y), :stroke)
	end
	for j=-Y:50:Y
		line(Point(-X,j),Point(X,j), :stroke)
	end
end
@svg begin
	X,Y=250,250
	sethue("black")
	box(Point(0, 0), 2X, 2Y, :fill)
	innerpoints = [Point(rand(120:160)*sin(i), rand(130:160)*cos(i)) for i in 0:0.1:2π]
	outerpoints = [Point(rand(190:200)*sin(i), rand(190:200)*cos(i)) for i in 0:0.1:2π]
	donut = [innerpoints; innerpoints[1]; outerpoints[1]; reverse(outerpoints)]
	poly(donut, :clip)
    sethue("black")
	drawGrid(X,Y)
	sethue("white")
	box(Point(0, 0), 2X, 2Y, :fill)
	clipreset()
	setcolor(0.5, 0.5, 0.5, 0.5)
	drawGrid(X,Y)
end

4 Likes