Skip to content

Instantly share code, notes, and snippets.

@briochemc
Created October 9, 2020 07:51
Show Gist options
  • Save briochemc/9fcf9610de935341b92508b33486609a to your computer and use it in GitHub Desktop.
Save briochemc/9fcf9610de935341b92508b33486609a to your computer and use it in GitHub Desktop.
### A Pluto.jl notebook ###
# v0.12.2
using Markdown
using InteractiveUtils
# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error).
macro bind(def, element)
quote
local el = $(esc(element))
global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : missing
el
end
end
# ╔═╡ 9b3bb46e-09f6-11eb-2cb3-f1bb4bafaf92
using Plots, PlutoUI
# ╔═╡ 9e9c3bd8-09f6-11eb-254a-7b5da9ccb433
md"""
nx $(@bind nx Slider(3:50, show_value=true, default=4))
ny $(@bind ny Slider(3:50, show_value=true, default=5))
nt $(@bind nt Slider(3:50, show_value=true, default=3))
"""
# ╔═╡ 94c3fbe6-09f6-11eb-36a0-f16c4b9be668
begin
xedges = [0; cumsum(rand(nx))]
xs = [0.5(xedges[i]+xedges[i+1]) for i in 1:nx]
yedges = [0; cumsum(rand(ny))]
ys = [0.5(yedges[i]+yedges[i+1]) for i in 1:ny]
vs = [rand(1:255) for x in xs, y in ys]
xt = sort(maximum(xedges) * rand(nt))
yt = sort(maximum(yedges) * rand(nt))
dx = 0.2
end
# ╔═╡ 2a319030-09f7-11eb-0cc7-892392706177
vs
# ╔═╡ f05c49fe-09f6-11eb-0cd5-17d97e9459e7
begin
plt = heatmap(xs, ys, vs', clims=(0,256), colorbar=false, title="heatmap")
#plot!(plt, xt, yt, lc=:green, m=:o)
xlims!(plt, (xedges[1] - dx, xedges[end] + dx))
ylims!(plt, (yedges[1] - dx, yedges[end] + dx))
vline!(plt, xs, lab="x centers")
hline!(plt, ys, lab="y centers")
end
# ╔═╡ d811b742-0a02-11eb-29a0-c55740f2b712
begin
plt4 = heatmap(xs, ys, vs', clims=(0,256), colorbar=false, title="heatmap")
#plot!(plt, xt, yt, lc=:green, m=:o)
xlims!(plt4, (xedges[1] - dx, xedges[end] + dx))
ylims!(plt4, (yedges[1] - dx, yedges[end] + dx))
vline!(plt4, xs, lab="pixel centers", c=:blue)
hline!(plt4, ys, lab="", c=:blue)
vline!(plt4, xedges, lab="pixel edges", c=:lightgray)
hline!(plt4, yedges, lab="", c=:lightgray)
end
# ╔═╡ 166857e6-09f7-11eb-030e-69c81b9eabcc
xedges
# ╔═╡ ab456962-09f7-11eb-3af7-81e12b0fd17b
begin
plt2 = edgyheatmap(xedges, yedges, vs, title="edgyheatmap")
vline!(plt2, xedges, lab="x edges")
hline!(plt2, yedges, lab="y edges")
end
# ╔═╡ ac461a4a-0a02-11eb-3daa-d587b60facae
begin
plt3 = edgyheatmap(xedges, yedges, vs, title="edgyheatmap")
vline!(plt3, xs, lab="pixel centers", c=:blue)
hline!(plt3, ys, lab="", c=:blue)
vline!(plt3, xedges, lab="pixel edges", c=:lightgray)
hline!(plt3, yedges, lab="", c=:lightgray)
end
# ╔═╡ 637b01be-0a01-11eb-3e17-3d906d5f257e
plot(plt4, plt3, layout=(2,1), link=:all, size=(500,600))
# ╔═╡ d0cbefc0-09f8-11eb-0fe3-d7ec4d4d7aca
@userplot EdgyHeatMap
# ╔═╡ 6b12e694-09fc-11eb-38b2-070fcb34adab
size(xedges), size(yedges), size(vs)
# ╔═╡ 1355dfb0-09fc-11eb-1b02-13e0f6e0d73a
# A heatmap that takes in the edges of its "pixels" (instead of their center)
@recipe function f(p::EdgyHeatMap)
ex, ey, z = p.args # extract args
clims --> extrema(z)
xlims --> (ex[1] - dx, ex[end] + dx)
ylims --> (ey[1] - dx, ey[end] + dx)
label --> ""
colorbar --> :right
# add a shape for each "pixel"
for I in CartesianIndices(z)
i, j = Tuple(I)
@series begin
seriestype := :shape
fillcolor := cgrad(:default)[z[i,j]]
linewidth := 0
label := ""
ex[[i, i, i+1, i+1, i]], ey[[j, j+1, j+1, j, j]]
end
end
()
end
# ╔═╡ Cell order:
# ╠═94c3fbe6-09f6-11eb-36a0-f16c4b9be668
# ╠═9b3bb46e-09f6-11eb-2cb3-f1bb4bafaf92
# ╟─9e9c3bd8-09f6-11eb-254a-7b5da9ccb433
# ╠═637b01be-0a01-11eb-3e17-3d906d5f257e
# ╠═2a319030-09f7-11eb-0cc7-892392706177
# ╠═f05c49fe-09f6-11eb-0cd5-17d97e9459e7
# ╠═d811b742-0a02-11eb-29a0-c55740f2b712
# ╠═166857e6-09f7-11eb-030e-69c81b9eabcc
# ╠═ab456962-09f7-11eb-3af7-81e12b0fd17b
# ╠═ac461a4a-0a02-11eb-3daa-d587b60facae
# ╠═d0cbefc0-09f8-11eb-0fe3-d7ec4d4d7aca
# ╠═6b12e694-09fc-11eb-38b2-070fcb34adab
# ╠═1355dfb0-09fc-11eb-1b02-13e0f6e0d73a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment