如何在 Julia 中使用 GLMakie 或 Plotlyjs 绘制圆柱体?

How to Plot a Cylinder with GLMakie or Plotlyjs in Julia?

提问人:Freya the Goddess 提问时间:12/27/2022 最后编辑:Freya the Goddess 更新时间:1/4/2023 访问量:205

问:

我想绘制一个三维圆柱体。

参数方程将如下所示:

x= r*cos(u)
y=r*sin(u)
z=v

$u \in [0, 2 \pi] $ 和 $v \in [0, h]$

我能够使用以下代码使用 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)

但是当我尝试制作圆柱体时,它失败了:

using GLMakie
GLMakie.activate!()
set_theme!(backgroundcolor = :white)

# A cylinder of radius r, and height h, having z-axis as symmetry axis
# it is a stack of circles of radius 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)

谁能帮我?

谢谢

编辑:(仍然需要帮助来完善情节)

我现在能够绘制一个圆柱体,但它的顶部和底部仍然是空心的,这是代码:

using Plots
plotlyjs()

# If x, y, z are vectors then it won't generate a surface
# for a parameterized surface x,y,z should be matrices:
# Check for: 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)'
#Surface parameterization
X = r*cos.(us)
Y = r*sin.(us)
Z = vs
Plots.surface(X, Y, Z, size=(600,600), cbar=:none, legend=false)
朱莉娅

评论


答:

1赞 Bill 1/4/2023 #1

指定的所有曲面的半径均为 5,因此不会填充端部的内部。不过,您还可以在两端绘制一个圆盘。

评论

0赞 Freya the Goddess 1/4/2023
好吧,那我就画一个圆盘..谢谢