如何在Julia中绘制3D热图?



我想绘制3D函数f(x,y,z)的3D热图。

对于2D函数f(x,y),我知道下面的代码可以工作。

using Plots
x = 1:L  # coordinate range
y = 1:L
F = Float64[f(ix,iy) for ix in x, iy in y]' #convert f(x,y) to an array
plot(F,st=:heatmap,color= cgrad(:blues))
plot!(xlabel="x",ylabel="y",aspect_ratio=:equal)
plot!(xlims=(1,L),ylims=(1,L))

对于3D功能,我应该在哪里更改?

using Plots
x = 1:L  # coordinate range
y = 1:L
z = 1:L
F = Float64[f(ix,iy,iz) for ix in x, iy in y,iz in z] #convert f(x,y,z) to an array
plot(F,st=:heatmap,color = cgrad(:blues),alpha=0.1)
plot!(xlabel="x",ylabel="y",zlabel="z",aspect_ratio=:equal)
plot!(xlims=(1,L),ylims=(1,L),zlims=(1,L))

这段代码通过了,但是有些地方出错了。
color = cgrad(:blues),alpha=0.1,xlabel="x",ylabel="y"不被反射。
另外,这个数字似乎不是f(x,y,z)。例如,f(x,y,z) = x^2 + y^2 +z^2给出一个球面渐变,但结果不是。

对于更多的数据点,上述方法速度较慢。但是,我认为你不需要热图,因为前面链接中的热图只是从2D到3D平面的投影。

我想你需要这样的东西。 https://lazarusa.github.io/BeautifulMakie/surfWireLines/volume/

见图片

为了方便,这里也是:

using GLMakie
let
x = 1:10
y = 1:10
z = 1:10
f(x,y,z) = x^2 + y^2 + z^2
vol = [f(ix,iy,iz) for ix in x, iy in y, iz in z]
fig, ax, _ = volume(x, y, z, vol, colormap = :plasma,colorrange = (minimum(vol), maximum(vol)),
figure = (; resolution = (800,800)),  
axis=(; type=Axis3, perspectiveness = 0.5,  azimuth = 7.19, elevation = 0.57,  
aspect = (1,1,1)))
fig
end

3D热图by Makie.jl
我不知道如何绘制3D热图。还没有,但我找到了另一条路。jl: https://lazarusa.github.io/BeautifulMakie/surfWireLines/RGBcube/.
在这个示例代码的帮助下,我得到了以下代码:

using GLMakie, GeometryBasics, Colors
positions = vec([(i, j, k) for i=1:L,j=1:L,k=1:L]) #3D coordinate
F = zeros(Float64,length(positions)
for i = 1:length(positions) #convert f(x,y,z) to an array
x = positions[i][1]
y = positions[i][2]
z = positions[i][3]
   F[i] = f(x,y,z)
end
fig, ax = mesh(HyperRectangle(Vec3f0(positions[1]...),Vec3f0(0.8)), color = RGBA(0,0,F[1],0.5), transparency = false) #HyperRectangle(::position,::length),color=(::red,::green,::blue,::alpha)
wireframe!(ax,HyperRectangle(Vec3f0(positions[1]...), Vec3f0(0.8)), linewidth = 0.1, overdraw = false)
for i in 2:length(positions)
mesh!(ax, HyperRectangle(Vec3f0(positions[i]...), Vec3f0(0.8)), color = RGBA(0,0,F[i],0.5))
wireframe!(ax, HyperRectangle(Vec3f0(positions[i]...), Vec3f0(0.8)), linewidth = 0.1, overdraw = false)
end
fig

最新更新