How to Create a 3D-Transparent Cylinder with GLMakie or Plots if possible?

Hi all,

I created this sphere with GL Makie:
Capture d’écran_2022-12-25_13-16-55

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)

I want to plot 3-D cylinder, like this
Capture d’écran_2022-12-25_13-19-12

Which equations of cylinder should I use? So I am able to plot it 3-dimensionally like the sphere above

you should have a look at cylindrical coordinates: Cylindrical coordinate system - Wikipedia

the section on coordinate system conversions should have the right formulas

1 Like

A cylinder of radius r, and height h, having z-axis as symmetry axis, is a stack of circles of radius r:

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

u \in [0, 2\pi], v \in [0, h]
Now you can plot it as any parameterized surface.

1 Like

I have been trying this code but fail to create a cylinder:

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)

Your code doesn’t generate a surface, because x, y, z are vectors, but for a parameterized surface they should be matrices:

using Plots
plotlyjs()

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)

Now check typeof(X), typeof(Y), typeof(Z)

I see, it is a matrix now:

typeof(X)

Matrix{Float64} (alias for Array{Float64, 2})

Hi there, I was wondering if this solution can be adapted to draw a transparent cylinder. Thanks!

1 Like

To get a transparent cylinder, set transparency, alpha =a, with a ∈ (0,1):

Plots.surface(X, Y, Z,  c=:red,  cbar=:none, legend=false, alpha=0.5)

To add the two boundary disks extend the code with the following lines:

p=10
R =range(0, 5, length=p)
x = cos.(u) *R'
y = sin.(u) *R'
surface!(x, y,  h*ones(size(x)), c=:red, alpha=0.5)
surface!(x, y,  zeros(size(x)), c=:red, alpha=0.5)

Anwered here:

Easy fix!

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 θ, _ in v]
y = [r*sin(θ) for θ in θ, _ in v]
z = [v for _ in θ, v in v]
surface(x, y, z)