# I Have PlotlyJS Codes to Plot Inverted Cone and Cylinder, How to Combine them into One?

**URL:** <https://discourse.julialang.org/t/i-have-plotlyjs-codes-to-plot-inverted-cone-and-cylinder-how-to-combine-them-into-one/92589>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [January 6, 2023, 6:27am UTC](https://discourse.julialang.org/t/i-have-plotlyjs-codes-to-plot-inverted-cone-and-cylinder-how-to-combine-them-into-one/92589 "2023-01-06T06:27:37Z")\
**Posts on this page:** 4\
**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:** [January 6, 2023, 6:27am UTC](https://discourse.julialang.org/t/i-have-plotlyjs-codes-to-plot-inverted-cone-and-cylinder-how-to-combine-them-into-one/92589/1 "2023-01-06T06:27:37Z")

</div>

Hi all,

I have code to plot each of the cylinder and cone from this discourse. How to combine them into one, so the inverted cone will be inside the cylinder?

![Capture d’écran_2023-01-06_13-24-33](https://global.discourse-cdn.com/julialang/original/3X/c/2/c2d6e45de229484060a50726a0d094c99198ca70.png)

The color needs to be adjusted as well, can we make a grid too? Tikz Latex can make grids with `foreach` command…

this is the codes for cylinder:

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

```

for inverted cone:

```julia
using Plots; plotlyjs()

X(r,theta) = r * cos(theta)
Y(r,theta) = r * sin(theta)
Z(r,theta) = r

rs = range(0, 2, length=50)
ts = range(0, 2pi, length=50)

surface(X.(rs',ts), Y.(rs', ts), Z.(rs', ts))

```

---

<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:** [January 6, 2023, 9:41am UTC](https://discourse.julialang.org/t/i-have-plotlyjs-codes-to-plot-inverted-cone-and-cylinder-how-to-combine-them-into-one/92589/2 "2023-01-06T09:41:49Z")

</div>

In the cone parameterization the radius is variable, and with your rs definition, the cone height, h=maximum(rs)=2. Hence we have to draw of cylinder of radius 2, and the same height, h=2. Since the cylinder radius is constant, it is not recommended to use r as parameter. I’m using u, v, that are “classical” variables for a surface parameterization, and thus avoid confusion between variable and constant radius for the two surfaces:

```julia
function cyl_cone(; h =2, m=50, n=50)
    #cone parameterizarion u∈[0, 2π], v∈[0, h]; 
    X(u,v) = v*cos(u)
    Y(u,v) = v*sin(u)
    Z(u,v) = v
    #cylinder circumscribed to the above cone; u∈[0, 2π], v∈[0, h]
    x(u, v) = h*cos(u)
    y(u, v) = h*sin(u)
    z(u, v) = v
    u=range(0, 2pi, length=m)
    v = range(0, h, length=n)
    plt =surface(x.(u, v'), y.(u, v'), ones(m) *v')
    surface!(plt, (X.(u, v'), Y.(u, v'), Z.(u, v')))
    return plt
end    
plt =cyl_cone(;)

```

---

<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:** [January 6, 2023, 2:37pm UTC](https://discourse.julialang.org/t/i-have-plotlyjs-codes-to-plot-inverted-cone-and-cylinder-how-to-combine-them-into-one/92589/3 "2023-01-06T14:37:12Z")

</div>

I add wireframe and it looks like Basketball cornello ice cream:

![Capture d’écran_2023-01-06_21-36-20](https://global.discourse-cdn.com/julialang/original/3X/c/3/c31a1d6df692d683715629c824cafa1f08b29889.png)

```julia
using Plots; plotlyjs()

function cyl_cone(; h =2, m=50, n=50, fillalpha=0.6)
    #cone parameterizarion u∈[0, 2π], v∈[0, h]; 
    X(u,v) = v*cos(u)
    Y(u,v) = v*sin(u)
    Z(u,v) = v
    #cylinder circumscribed to the above cone; u∈[0, 2π], v∈[0, h]
    x(u, v) = h*cos(u)
    y(u, v) = h*sin(u)
    z(u, v) = v
    u=range(0, 2pi, length=m)
    v = range(0, h, length=n)
    plt =surface(x.(u, v'), y.(u, v'), ones(m) *v', fillalpha=0.7)
    surface!(plt, (X.(u, v'), Y.(u, v'), Z.(u, v')))
    wireframe!(plt, (X.(u, v'), Y.(u, v'), Z.(u, v')))
    return plt
end    
plt =cyl_cone(;)

```

---

<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:** [January 6, 2023, 3:31pm UTC](https://discourse.julialang.org/t/i-have-plotlyjs-codes-to-plot-inverted-cone-and-cylinder-how-to-combine-them-into-one/92589/4 "2023-01-06T15:31:14Z")

</div>

To get the right wireframe you should plot a number p, of cone generatrices, and a number q of circles.  
I explain how to get these lines, because just now I have no access to a computer to write the corresponding code:

```julia
 p, q = 12, 10
 u= range(0, 2pi, length=p)
v = range( 0, h, length=q)

```

For each uw= u[k], k =1,…,p

```julia
x=v*cos(uw)
y=v*sin(uw)
z=v

```

give the points on a cone generatrice, while  
for each vw=v[j], j=1,…,q

```julia
X= vw*cos(u)
Y=vw*sin(u)
Z= vw

```

we get the points on a cone circle.
