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

**URL:** <https://discourse.julialang.org/t/how-to-create-a-3d-transparent-cylinder-with-glmakie-or-plots-if-possible/92099>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [December 25, 2022, 6:20am UTC](https://discourse.julialang.org/t/how-to-create-a-3d-transparent-cylinder-with-glmakie-or-plots-if-possible/92099 "2022-12-25T06:20:45Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [December 25, 2022, 6:20am UTC](https://discourse.julialang.org/t/how-to-create-a-3d-transparent-cylinder-with-glmakie-or-plots-if-possible/92099/1 "2022-12-25T06:20:45Z")

</div>

Hi all,

I created this sphere with GL Makie:  
 ![Capture d’écran_2022-12-25_13-16-55](https://global.discourse-cdn.com/julialang/original/3X/4/e/4e92d36a49e5de7f09a9906614d79aed866826de.png)

```julia
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](https://global.discourse-cdn.com/julialang/original/3X/6/0/60bff4abe8563625335639ab8c2cdf97adde6b79.png)

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

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [December 25, 2022, 5:29pm UTC](https://discourse.julialang.org/t/how-to-create-a-3d-transparent-cylinder-with-glmakie-or-plots-if-possible/92099/2 "2022-12-25T17:29:57Z")

</div>

you should have a look at cylindrical coordinates: [Cylindrical coordinate system - Wikipedia](https://en.m.wikipedia.org/wiki/Cylindrical_coordinate_system)

the section on coordinate system conversions should have the right formulas

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [December 25, 2022, 7:56pm UTC](https://discourse.julialang.org/t/how-to-create-a-3d-transparent-cylinder-with-glmakie-or-plots-if-possible/92099/3 "2022-12-25T19:56:40Z")

</div>

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [December 27, 2022, 9:28am UTC](https://discourse.julialang.org/t/how-to-create-a-3d-transparent-cylinder-with-glmakie-or-plots-if-possible/92099/4 "2022-12-27T09:28:11Z")

</div>

> [@Freya\_the\_Goddess](#):
>
> ```julia
> 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 have been trying this code but fail to create a cylinder:

```julia
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)

```

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [December 27, 2022, 9:57am UTC](https://discourse.julialang.org/t/how-to-create-a-3d-transparent-cylinder-with-glmakie-or-plots-if-possible/92099/5 "2022-12-27T09:57:30Z")

</div>

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

```julia
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)

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [December 27, 2022, 10:01am UTC](https://discourse.julialang.org/t/how-to-create-a-3d-transparent-cylinder-with-glmakie-or-plots-if-possible/92099/6 "2022-12-27T10:01:35Z")

</div>

I see, it is a matrix now:

```julia
typeof(X)

```

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

---

<div class="post-metadata">

**Author:** ![jjgomezcadenas](https://avatars.discourse-cdn.com/v4/letter/j/50afbb/32.png) [@jjgomezcadenas](https://discourse.julialang.org/u/jjgomezcadenas)\
**Post date:** [May 29, 2023, 7:28pm UTC](https://discourse.julialang.org/t/how-to-create-a-3d-transparent-cylinder-with-glmakie-or-plots-if-possible/92099/7 "2023-05-29T19:28:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [May 29, 2023, 9:12pm UTC](https://discourse.julialang.org/t/how-to-create-a-3d-transparent-cylinder-with-glmakie-or-plots-if-possible/92099/8 "2023-05-29T21:12:54Z")

</div>

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

```julia
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:

```julia
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)

```

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [May 29, 2023, 11:34pm UTC](https://discourse.julialang.org/t/how-to-create-a-3d-transparent-cylinder-with-glmakie-or-plots-if-possible/92099/9 "2023-05-29T23:34:41Z")

</div>

Anwered here:

> [@How to render a cylinder?](https://discourse.julialang.org/t/how-to-render-a-cylinder/98063/14):
>
> From the docs: using Meshes, MeshViz import GLMakie as Mke c = Cylinder(1.0) viz(c) [image] Here is a collection of random cylinders: cs = [Cylinder(1.0, Segment(rand(Point3, 2))) for i=1:3] viz(cs, color=1:3) [image]

---

<div class="post-metadata">

**Author:** ![xor0110](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xor0110/32/7926_2.png) [@xor0110](https://discourse.julialang.org/u/xor0110)\
**Post date:** [October 5, 2023, 6:57pm UTC](https://discourse.julialang.org/t/how-to-create-a-3d-transparent-cylinder-with-glmakie-or-plots-if-possible/92099/10 "2023-10-05T18:57:16Z")

</div>

Easy fix!

```r
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)

```
