Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

Plotting values on a regular 2D grid in Julia

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)

enter image description here

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?

question from:https://stackoverflow.com/questions/65918661/plotting-values-on-a-regular-2d-grid-in-julia

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 NaNs 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!

givesenter image description here


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

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...