如何在Julia中使用GLMakie或Plotlyjs绘制三维圆柱体?
问题:GLMakie绘制三维圆柱体失败,Plots版本存在上下空心问题
圆柱体参数方程
目标绘制的圆柱体参数方程为:
x= r*cos(u) y=r*sin(u) z=v
其中 $u \in [0, 2 \pi] $,$v \in [0, h]$。
已成功实现的GLMakie球体绘制代码
此前已用GLMakie成功绘制球体,代码如下:
using GLMakie n = 20 θ = [0;(0.5:n-0.5)/n;1] φ = [(0:2n-2)*2/(2n-1);2] x = [cospi(φ)*sinpi(θ) for θ in θ, φ in φ] y = [sinpi(φ)*sinpi(θ) for θ in θ, φ in φ] z = [cospi(θ) for θ in θ, φ in φ] surface(x, y, z)
失败的GLMakie圆柱体绘制代码
尝试用GLMakie绘制圆柱体时失败,代码如下:
using GLMakie GLMakie.activate!() set_theme!(backgroundcolor = :white) # 半径r、高度h的圆柱体,以z轴为对称轴 # 由半径r的圆堆叠而成 r = 5 h = 3 n = 50 θ = LinRange(0, 2pi, 100) #θ = [0;(0.5:n-0.5)/n;2π] v = [0;(1:n)/n;h] x = [r*cos(θ) for θ in θ] y = [r*sin(θ) for θ in θ] z = [v for v in v] surface(x, y, z)
补充:Plots版本实现(存在上下空心问题)
目前已通过Plots绘制出圆柱体,但上下部分为空心,代码如下:
using Plots plotlyjs() # 若x、y、z为向量则无法生成曲面 # 参数化曲面要求x、y、z为矩阵: # 可检查typeof(X)、typeof(Y)、typeof(Z) r = 5 h = 3 m, n =200, 150 u = range(0, 2pi, length=n) v = range(0, h, length=m) us = ones(m)*u' vs = v*ones(n)' # 曲面参数化 X = r*cos.(us) Y = r*sin.(us) Z = vs Plots.surface(X, Y, Z, size=(600,600), cbar=:none, legend=false)
内容的提问来源于stack exchange,提问作者Freya the Goddess
相关产品推荐
相关产品推荐

