With development of spatial transfriptomics, we can get the spatial information of gene expression. However, the data is 2D, and it is hard to visualize the data in 3D. Here, I will introduce some methods to visualize the data in 3D.

But for the first time, there are only some visualization method, next i will introduce some methods.

here is the code, which is easy for understanding.

def plot_cloud_point(adata: ad.AnnData, 
                     spatial_key: str='3d_align_spatial',
                     anno: str='region',
                     color_anno: str='color_anno',
                     color_map: Optional[dict] = None,
                     point_size: Union[float, list] = 20,
                     save_path: Optional[str] = None
    # position_s = adata.obsm['3d_align_spatial']
    # color_s = [color_map[i] for i in adata.obs[anno].tolist()]
    # point_size_s = [i**0.5 for i in adata.obs['area'].tolist()]
    if color_map is None:
        color_map = adata.uns[color_anno]
    ## 01. hexadecimal conversion
    annotation = list(color_map.keys())
    colors = np.array(list(color_map.values()))
    color_arr = np.array([int(c[1:], 16) for c in colors], dtype=np.uint32)
    color_map = dict(zip(annotation, color_arr))

    ## 02.generate  
    pts_map = {}
    #color_list = []
    point_cloud = np.empty((0,3), float)
    point_cloud = np.append(point_cloud, adata.obsm[spatial_key], axis=0)
    for i in range(len(adata.obs)):
        annot = adata.obs[anno][i]
        if annot not in pts_map:
            pts_map[annot] = np.empty((0,3), float)
        pts_map[annot] = np.append(pts_map[annot], [adata.obsm[spatial_key][i]], axis=0)
    ## 03. plot k3d
    plot = k3d.plot()
    for key, val in sorted(pts_map.items()):
        plt_points = k3d.points(positions=val,
                                colors = [color_map[key]]*val.shape[0],
                                # shader='3dSpecular',
                                opacity= 1,
                                name = key,
        plot += plt_points
    with open(save_path,'w') as fp:
    return plot

for this code, i used the k3d package, which is a 3D visualization package. but it's helpful for spatial transcriptomics data.

for the package import, you can use the following code:

import scanpy as sc
import pandas as pd
import numpy as np
import anndata as ad
from typing import Optional,Union
import k3d

you can download some 3d spatial transcriptomics data to make a visualization.