Skip to content

Note

Click here to download the full example code

Coloring#

This tutorial demonstrates how to adjust colors in NAVis plots.

By now, you should already have a basic understanding on how to plot neurons in NAVis (2d vs 3d plots, the various backends and plotting methods, etc.) - if not, check out the plotting tutorial.

In this tutorial we will focus on how to finetune these plots by changing colors 🌈. We will demonstrate this using matplotlib (plot2d) and plotly (plot3d) but everything shown here also works for the other backends (Octarine, Vispy and K3d)!

Neuron Colors#

Undoubtedly one of the most important aspects of a plot is the color scheme. In NAVis you can control the color of individual neurons, their compartments, synapses, etc. in a variety of ways. Here are a few examples that should get you started:

import navis
import matplotlib.pyplot as plt

nl = navis.example_neurons(3, kind="mesh")

A single color for all neurons:

navis.plot2d(nl, color="r", view=("x", "-z"), method="2d")
plt.tight_layout()

plot 01 plotting colors

A list of colors for each neuron:

navis.plot2d(nl, color=["r", "g", "b"], view=("x", "-z"), method="2d")
plt.tight_layout()

plot 01 plotting colors

A palette to choose colors from:

navis.plot2d(nl, palette="Greens", view=("x", "-z"), method="2d")
plt.tight_layout()

plot 01 plotting colors

A mapping of neuron ID -> color:

colors = dict(zip(nl.id, ["r", "g", "b"]))
navis.plot2d(nl, color=colors, view=("x", "-z"), method="2d")
plt.tight_layout()

plot 01 plotting colors

Individual colors can be provided as:

  • names (e.g. "red", "green", "blue") like we did above
  • hex codes (e.g. "#FF0000", "#00FF00", "#0000FF")
  • RGB or RGBA tuples (e.g. (1, 0, 0) for red)
# Provide a list of 3 colors - one for each neuron - in various formats:
navis.plot2d(nl, color=["red", "#FF0000", (0, 0, 0)], view=("x", "-z"), method="2d")
plt.tight_layout()

plot 01 plotting colors

What if you want to color neurons by some categorical property - for example their type or brain region? Easy peasy: just use the color_by parameter!

# A list with labels, one for each neuron
types = ["typeA", "typeB", "typeA"]

navis.plot2d(nl, color_by=types, palette="tab10", view=("x", "-z"), method="2d")
plt.tight_layout()

plot 01 plotting colors

NAVis automatically assigns a color to each unique label using the provided palette. You can also provide a dictionary to manually set the colors:

palette = {"typeA": "red", "typeB": "blue"}

navis.plot2d(nl, color_by=types, palette=palette, view=("x", "-z"), method="2d")
plt.tight_layout()

plot 01 plotting colors

Coloring Neurites#

So far so good but what if you want to color the neurites of an individual neuron? For example make its axon red and its dendrites blue? Also easy peasy: color_by can also be used to color nodes/vertices!

By Labels#

n = navis.example_neurons(1, kind="skeleton")

# This will add a "compartment" for each node in the neuron
navis.split_axon_dendrite(n, label_only=True)

n.nodes.head()
node_id label x y z radius parent_id type compartment
0 1 0 15784.0 37250.0 28062.0 10.000000 -1 root linker
1 2 0 15764.0 37230.0 28082.0 18.284300 1 slab linker
2 3 0 15744.0 37190.0 28122.0 34.721401 2 slab linker
3 4 0 15744.0 37150.0 28202.0 34.721401 3 slab linker
4 5 0 15704.0 37130.0 28242.0 34.721401 4 slab linker

Now we can color the neuron based on the "compartment" label:

navis.plot2d(n, color_by="compartment", palette="tab10", view=("x", "-z"), method="2d")
plt.tight_layout()

plot 01 plotting colors

We can also set the colors manually:

colors = {"axon": "coral", "dendrite": "cyan", "linker": "limegreen"}
navis.plot2d(n, color_by="compartment", palette=colors, view=("x", "-z"), method="2d")
plt.tight_layout()

plot 01 plotting colors

By Values#

You can also color neurites based on some numerical value. This is especially useful for things like Strahler index, branch order, etc.

Coloring by e.g. Strahler index:

n = navis.example_neurons(1, kind="skeleton")

# This adds an `strahler_index` column to the node table
navis.strahler_index(n)

n.nodes.head()
node_id label x y z radius parent_id type strahler_index
0 1 0 15784.0 37250.0 28062.0 10.000000 -1 root 6
1 2 0 15764.0 37230.0 28082.0 18.284300 1 slab 6
2 3 0 15744.0 37190.0 28122.0 34.721401 2 slab 6
3 4 0 15744.0 37150.0 28202.0 34.721401 3 slab 6
4 5 0 15704.0 37130.0 28242.0 34.721401 4 slab 6

Plot with color based on Strahler index:

navis.plot2d(
    n, color_by="strahler_index", palette="viridis", view=("x", "-z"), method="2d"
)
plt.tight_layout()

plot 01 plotting colors

Note

You can use the vmin and vmax parameters to control the normalization of the color scale.

All of this also works with MeshNeurons. Here, we have to provide a label for each vertex in the mesh:

m = navis.example_neurons(1, kind="mesh")
navis.strahler_index(m)
m.strahler_index  # this is an array with one value per vertex

Out:

/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/skeletor/skeletonize/wave.py:198: DeprecationWarning:

Graph.clusters() is deprecated; use Graph.connected_components() instead

/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/skeletor/skeletonize/wave.py:228: DeprecationWarning:

Graph.shortest_paths() is deprecated; use Graph.distances() instead


array([2, 3, 3, ..., 1, 4, 4])
# Let's use plot3d this time
navis.plot3d(m, color_by="strahler_index", palette="viridis", legend=False)

Note

In the examples above we have provided color_by as the name of a property or a column in the node table. We could have also provided an array of values directly:

navis.plot2d(n, color_by=n.nodes.strahler_index, palette="viridis")

Neuron Opacity#

You can also control the opacity of the neurons. This is especially useful when plotting multiple neurons on top of each other:

# A single opacity for all neurons
navis.plot2d(nl, alpha=0.25, view=("x", "-z"), method='2d')
plt.tight_layout()

plot 01 plotting colors

# A list of alpha values, one for each neuron
navis.plot2d(nl, color="k", alpha=[1, 0.2, 0.2], view=("x", "-z"), method='2d')
plt.tight_layout()

plot 01 plotting colors

Shading#

Analogous to the color_by parameter, you can also shade neurons based on some property. Let's demonstrate this by shading a neuron based on its distance from the soma:

n = navis.example_neurons(1, kind="skeleton")
n.reroot(n.soma, inplace=True)
n.nodes["root_dist"] = n.nodes.node_id.map(navis.dist_to_root(n, weight="weight")) * -1
n.nodes.head()
node_id label x y z radius parent_id type root_dist
0 1 0 15784.0 37250.0 28062.0 10.000000 2 end -1302.983639
1 2 0 15764.0 37230.0 28082.0 18.284300 3 slab -1268.342622
2 3 0 15744.0 37190.0 28122.0 34.721401 4 slab -1208.342622
3 4 0 15744.0 37150.0 28202.0 34.721401 5 slab -1118.899903
4 5 0 15704.0 37130.0 28242.0 34.721401 6 slab -1058.899903
# Plot with shading based on distance from the soma:
navis.plot2d(n, shade_by="root_dist", view=("x", "-z"), method="2d")
plt.tight_layout()

plot 01 plotting colors

We can combine color_by and shade_by to color and shade the neuron at the same time:

navis.plot2d(
    n,
    color_by="root_dist",
    palette="viridis",
    shade_by="root_dist",
    view=("x", "-z"),
    method="2d",
)
plt.tight_layout()

plot 01 plotting colors

Total running time of the script: ( 0 minutes 13.626 seconds)

Download Python source code: plot_01_plotting_colors.py

Download Jupyter notebook: plot_01_plotting_colors.ipynb

Gallery generated by mkdocs-gallery