Reputation: 611
There is a certain parameter values set over a regular 2D lattice. I want to display them in the form of cells of a regular grid, filled in with a color depending of the value of the parameter. There may be gaps in individual grid nodes. Here is an example of the data and the desired result of plotting:
a = [1, 2, 3, 1, 2, 3, 1, 2]
b = [1, 1, 1, 2, 2, 2, 3, 3]
c = [1, 5, 4, 3, 4, 2, 1, 3]
plot(a, b, zcolor = c, aspect_ratio = 1, xlim = (0.5, 3.5), ylim = (0.5, 3.5), clim = (0, 5),
seriestype = :scatter, markersize = 82, markershape = :square, markerstrokewidth = 0.5,
legend = false, colorbar = true)
This approach works, but in this case, is needed to adjust the size of the squares each time so that there are no gaps between the cells and they do not run over each other. This requires constant manual intervention and does not look like the right solution. What is the most correct approach in this case? I thought about heatmap(), but as far as I understand, in Julia it does not know how to display cell borders. Perhaps there is some way to set in plot() the icon sizes in absolute canvas units? Or is it better to use some other approach for such situations?
Upvotes: 3
Views: 1814
Reputation: 2995
heatmap
seems like the way to go to me. If your a
and b
spanned the entire lattice, I would just use reshape
to, well, reshape c
. This is not your case here, so one solution is to fill the lattice with NaN
s where you have no value. E.g.,
using Plots
a = [1, 2, 3, 1, 2, 3, 1, 2]
b = [1, 1, 1, 2, 2, 2, 3, 3]
c = [1, 5, 4, 3, 4, 2, 1, 3]
x, y = sort(unique(a)), sort(unique(b)) # the lattice x and y
C = fill(NaN, length(y), length(x)) # make a 2D C with NaNs
for (i,j,v) in zip(a,b,c) # fill C with c values
C[j,i] = v
end
heatmap(x, y, C, clims=(0,5)) # success!
EDIT: If you want to specify the edges of the cells, you can do this with heatmap
, and if you want lines, you can add them manually I guess, for example with vline
and hline
?
ex, ey = [0, 1.5, 2.3, 4], [0.5, 1.5, 2.5, 3.5]
heatmap(ex, ey, C, clims=(0,5))
vline!(ex, c=:black)
hline!(ey, c=:black)
will give
Upvotes: 3