如何在 Julia 中绘制 3D 热图?
How to plot 3D HeatMap in Julia?
我想为 3D 函数 f(x,y,z) 绘制 3D 热图。
对于二维函数 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给出的是球形渐变,结果却不是。
3D 热图 Makie.jl
我还不知道如何通过 Plots.jl 绘制 3D 热图,但我通过 Makie.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
以上方法对于更多数据点来说很慢。但是,我认为您不需要热图,因为之前 link 中的热图只是从 2D 到 3D 平面的投影。
我想你需要这样的东西。
请参阅此处的代码。
https://lazarusa.github.io/BeautifulMakie/surfWireLines/volume/
See image
为了方便起见,也在这里:
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 函数 f(x,y,z) 绘制 3D 热图。
对于二维函数 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给出的是球形渐变,结果却不是。
3D 热图 Makie.jl
我还不知道如何通过 Plots.jl 绘制 3D 热图,但我通过 Makie.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
以上方法对于更多数据点来说很慢。但是,我认为您不需要热图,因为之前 link 中的热图只是从 2D 到 3D 平面的投影。
我想你需要这样的东西。 请参阅此处的代码。
https://lazarusa.github.io/BeautifulMakie/surfWireLines/volume/
See image
为了方便起见,也在这里:
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