# How to produce a 3D polar plot in Makie?

**URL:** <https://discourse.julialang.org/t/how-to-produce-a-3d-polar-plot-in-makie/107950>\
**Category:** Visualization\
**Tags:** plotting, makie\
**Created:** [December 22, 2023, 3:28pm UTC](https://discourse.julialang.org/t/how-to-produce-a-3d-polar-plot-in-makie/107950 "2023-12-22T15:28:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daptine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daptine/32/50372_2.png) [@Daptine](https://discourse.julialang.org/u/Daptine)\
**Post date:** [December 22, 2023, 3:28pm UTC](https://discourse.julialang.org/t/how-to-produce-a-3d-polar-plot-in-makie/107950/1 "2023-12-22T15:28:38Z")

</div>

I wonder how to produce plots like the one below (a 3D array factor) using Makie?  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/9/099671c82acbc02935768ce7f297d28f36451c14.jpeg)

If this is not possible in Makie, any packages that support this? Thanks a lot.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [December 22, 2023, 3:35pm UTC](https://discourse.julialang.org/t/how-to-produce-a-3d-polar-plot-in-makie/107950/2 "2023-12-22T15:35:45Z")

</div>

I would not call this a polar plot because it is not using a polar axis. You can plot the mesh you have there like any other, even if its generation involves a polar coordinate system.

---

<div class="post-metadata">

**Author:** ![Salmon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/salmon/32/22968_2.png) [@Salmon](https://discourse.julialang.org/u/Salmon)\
**Post date:** [December 22, 2023, 5:02pm UTC](https://discourse.julialang.org/t/how-to-produce-a-3d-polar-plot-in-makie/107950/3 "2023-12-22T17:02:02Z")

</div>

To expand on the answer already given, Check out the [beautiful makie gallery](http://beautiful.makie.org/dev/examples/generated/2d/surfaces/torus/) for examples.

---

<div class="post-metadata">

**Author:** ![mike.ingold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike.ingold/32/203749_2.png) [@mike.ingold](https://discourse.julialang.org/u/mike.ingold)\
**Post date:** [December 22, 2023, 6:41pm UTC](https://discourse.julialang.org/t/how-to-produce-a-3d-polar-plot-in-makie/107950/4 "2023-12-22T18:41:10Z")

</div>

Here’s a quick-and-dirty attempt I just threw together with Makie that you could play around with:

```julia
using GLMakie

AF(θ,φ) = abs( sinc(2θ) )

# Coordinate space to be modeled
φs = range(0, 2π, length=361) # angle within xy-plane relative to +x
θs = range(0, π, length=181) # angle relative to +z
angles = [(φ,θ) for φ in φs, θ in θs]

# Get radius (the Array Factor) at each angle
rs = [AF(θ, φ) for (φ,θ) in angles] # radius = AF(θ,φ)

# Convert this data to a 2D mesh
spherical_mesh = [(r,φ,θ) for (r,(φ,θ)) in zip(rs,angles)]

# Convert spherical coordinates to rectangular coordinates
xs = [r*sin(θ)*cos(φ) for (r,φ,θ) in spherical_mesh]
ys = [r*sin(θ)*sin(φ) for (r,φ,θ) in spherical_mesh]
zs = [r*cos(θ) for (r,φ,θ) in spherical_mesh]

# Plot
fig = Figure()
ax = Axis3(fig[1,1])
surface!(ax, xs, ys, zs)
save("antennapattern.png", fig)

```

![antennapattern](https://global.discourse-cdn.com/julialang/original/3X/3/1/31f171f44e6cd1e97b8fad315fb98cc7fe78c79a.png)

Not exactly the prettiest pattern, but you get the idea.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 22, 2023, 7:44pm UTC](https://discourse.julialang.org/t/how-to-produce-a-3d-polar-plot-in-makie/107950/5 "2023-12-22T19:44:32Z")

</div>

> [@jules](#):
>
> I would not call this a polar plot

Perhaps, it should be called a 3D spherical plot?  
[see: SphericalPlot3D—Wolfram Language Documentation](https://reference.wolfram.com/language/ref/SphericalPlot3D.html)

---

<div class="post-metadata">

**Author:** ![mike.ingold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike.ingold/32/203749_2.png) [@mike.ingold](https://discourse.julialang.org/u/mike.ingold)\
**Post date:** [December 22, 2023, 8:29pm UTC](https://discourse.julialang.org/t/how-to-produce-a-3d-polar-plot-in-makie/107950/6 "2023-12-22T20:29:44Z")

</div>

Kind of a technicality, but I think what @jules was trying to say is that in Makie a [`PolarAxis`](https://docs.makie.org/stable/reference/blocks/polaraxis/) specifically denotes an (r,theta) style of plot vs an [`Axis`](https://docs.makie.org/stable/reference/blocks/axis/) (2D x,y) or [`Axis3`](https://docs.makie.org/stable/reference/blocks/axis3/) (3D x,y,z). The example provided in the OP is technically a `surface` plot on an `Axis3`.

In common usage, though, it would be nice to have some recipe like `sphericalplot(f)` where `f(theta,phi)` is defined.

---

<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 22, 2023, 10:40pm UTC](https://discourse.julialang.org/t/how-to-produce-a-3d-polar-plot-in-makie/107950/7 "2023-12-22T22:40:09Z")

</div>

Plot of the surface r=1+2cos(2\varphi) with PlotlyJS:

```julia
using PlotlyJS
φ=range(0, π, 150)
θ=range(0,2π, 150)
x = @. cos(θ)*((1+2*cos(2*φ'))*sin(φ')) 
y = @. sin(θ)*((1+2*cos(2*φ'))*sin(φ'))
z = ones(150) *(1 .+2*cos.(2*φ')) .*cos.(φ')
pl=Plot(surface(x=x, y=y, z=z, colorbar=attr(thickness=24, len=0.75)), 
        Layout(width=500, height=500, font_size=11, 
               scene_camera_eye=attr(x=2.2, y=2.2, z=1.2)))

```

![spherical-plot-math](https://global.discourse-cdn.com/julialang/original/3X/2/d/2d40a8e767df801b49242630504b0cada075b0f8.png)

---

<div class="post-metadata">

**Author:** ![Daptine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daptine/32/50372_2.png) [@Daptine](https://discourse.julialang.org/u/Daptine)\
**Post date:** [December 23, 2023, 12:42am UTC](https://discourse.julialang.org/t/how-to-produce-a-3d-polar-plot-in-makie/107950/8 "2023-12-23T00:42:16Z")

</div>

Thank you for all the suggestions! especially @mike.ingold and @empet for the demo, I learned a lot.
