# How to visualize a 3D slice of a higher-dimensional space

**URL:** <https://discourse.julialang.org/t/how-to-visualize-a-3d-slice-of-a-higher-dimensional-space/99610>\
**Category:** Visualization\
**Tags:** geometry\
**Created:** [May 30, 2023, 2:49pm UTC](https://discourse.julialang.org/t/how-to-visualize-a-3d-slice-of-a-higher-dimensional-space/99610 "2023-05-30T14:49:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [May 30, 2023, 2:49pm UTC](https://discourse.julialang.org/t/how-to-visualize-a-3d-slice-of-a-higher-dimensional-space/99610/1 "2023-05-30T14:49:41Z")

</div>

A few months ago, I read a book called _The Elegant Universe_ and in it the author discusses [Calabi-Yau manifolds](https://en.wikipedia.org/wiki/Calabi%E2%80%93Yau_manifold) and includes a plot that is a 2D slice of a Calabi-Yau manifold. Does anyone know how to go about taking a 2D or 3D slice of a 4D (or higher) manifold and visualizing it? For example, in the Wikipedia link they provide this image which is a 2D slice of a 6D Calabi-Yau manifold:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/0/004b4b00a81a3830495f8b96b8ac6fa039b20dc9.jpeg)

How could one recreate this or, even better, create a 3D version?

---

<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 30, 2023, 6:26pm UTC](https://discourse.julialang.org/t/how-to-visualize-a-3d-slice-of-a-higher-dimensional-space/99610/2 "2023-05-30T18:26:27Z")

</div>

I think that what you are calling slices are in fact projections of the real representation of a Calabi Yau manifold, as a complex manifold, onto euclidian spaces of lower dimension. Example:  
z\_1^n+z\_2^n=1, n positive integer

This manifold consists in n^2 “submanifolds” represented by:  
z\_1=e^{2\pi i j/n}\cos(x+iy), j =0:n-1,  
z\_2=e^{2\pi i k/n}\sin(x+iy), k=0:n-1.  
A 4D \mathbb{R}-vector (real(z\_1), real(z\_2, imag(z\_1), imag(z\_2)) can be projected onto the 3d space in various modes.

For example projection to:

1. (real(z\_1), real(z\_2), imag(z\_1)) and coloring the corresponding surface according to the values of imag(z\_2)
2. (real(z\_1), real(z\_2, imag(z\_1)\*cos(a)+imag(z\_2)\*sin(a)) where a is an arbitrary angle; Here (imag(z\_1), imag(z\_2)) is projected onto the direction (cos(a), sin(a))  
etc, etc.

I illustrate these two cases with a PlotlyJS code, that can be adapted to be plotted with another plotting library:

```julia
using PlotlyJS

function calabi_yau(z::T, n::S, j::S, k::S) where {T<:Complex, S<:Integer}
    #z=x+1im y
    z1 = cis(2π*j/n) * cos(z)^(2/n)
    z2 = cis(2π*k/n) * sin(z)^(2/n)
    return [real(z1), real(z2), imag(z1), imag(z2)]   
end    

x = range(0, π/2, length=8)
y = range(-π/2, π/2, length=16)
z = [s for t in y, s in x] .+ 1im*[t for t in y, s in x];
surfs = GenericTrace{Dict{Symbol, Any}}[]
n=3
for (j, k) in Iterators.product(0:n-1, 0:n-1)
        xyzu = calabi_yau.(z, n, j, k; )
        
        push!(surfs, surface(x=getindex.(xyzu,1), y=getindex.(xyzu,2), 
                             z=getindex.(xyzu,3), surfacecolor=getindex.(xyzu,4),
                             coloraxis="coloraxis"))
        #or push!(surfs, surface(x=getindex.(xyzu,1), y=getindex.(xyzu,2), 
        # z=getindex.(xyzu,3) * cos(π/6) .+ getindex.(xyzu,4) * sin(π/6),
        # coloraxis="coloraxis"))
    end 
pl=Plot(surfs, Layout(width=600, height=600, font_size=11,
                   coloraxis=attr(colorscale=colors.plasma,
                                  colorbar_thickness=24, colorbar_len=0.7), 
                   scene_camera_eye=attr(x=1.8, y=1.8, z=1)))    

```

![Calabi-Yau](https://global.discourse-cdn.com/julialang/original/3X/4/e/4e4b1dda912bafde3b33394f14b0bc8e52758405.png)  
In your posted image each subsurface is colored with a distinct color. Here I used a colorscheme for the global surface.
