ggsegpy

Brain atlas visualization in Python

Three lines to visualize a brain atlas:

from plotnine import ggplot
from ggsegpy import geom_brain, dk

ggplot() + geom_brain(atlas=dk())

ggsegpy is a Python port of the R ggseg ecosystem. It provides 2D visualization with plotnine and 3D interactive visualization with Plotly.

Installation

pip install ggsegpy

What’s included

Three atlases ship with the package:

Atlas Type Regions
dk() Cortical Desikan-Killiany parcellation (34 × 2 hemispheres)
aseg() Subcortical FreeSurfer subcortical segmentation
tracula() White matter TRACULA tract atlas

Each works in both 2D and 3D:

from ggsegpy import aseg

ggplot() + geom_brain(atlas=aseg())

from ggsegpy import ggseg3d, aseg, pan_camera, add_glassbrain

fig = ggseg3d(atlas=aseg())
fig = add_glassbrain(fig, opacity=0.1)
fig = pan_camera(fig, "left lateral")
fig

Adding your data

Pass a DataFrame with a label column matching atlas regions:

from plotnine import ggplot, aes
import pandas as pd
from ggsegpy import geom_brain, dk

my_data = pd.DataFrame({
    "label": ["lh_precentral", "lh_postcentral", "rh_precentral", "rh_postcentral"],
    "thickness": [2.5, 2.1, 2.4, 2.0]
})

ggplot(my_data) + geom_brain(atlas=dk(), mapping=aes(fill="thickness"))

Learn more

Part of the ggseg ecosystem

ggsegpy is part of a larger ecosystem for brain visualization: