Skip to content

Instantly share code, notes, and snippets.

@valosekj
Last active March 19, 2025 18:28
Show Gist options
  • Save valosekj/e9464071a07197f52a4526d8014e2e9a to your computer and use it in GitHub Desktop.
Save valosekj/e9464071a07197f52a4526d8014e2e9a to your computer and use it in GitHub Desktop.
Load a Python colormap to FSLeyes

Load a Python colormap to FSLeyes

Note

You can directly pass the name of an matplotlib colormap to FSLeyes on the command line, for example:

fsleyes sub-001_T2w_rootlets.nii.gz -cm Jet

Thanks to Joshua for bringing this to my attention.


Get RGB colors, one per line, for a given Python colormap. In the script below, we get RGB colors for the jet matplotlib colormap.

import matplotlib.pyplot as plt
import numpy as np

# Generate the jet colormap
cmap = plt.cm.get_cmap('jet')

# Retrieve the RGB values for specific points in the colormap
colors = []
for i in np.linspace(0, 1, 20):
    color = cmap(i)[:3]  # Extract RGB values
    colors.append(color)

# Print the RGB values in the desired format
for color in colors:
    print(f"{color[0]:.6f} {color[1]:.6f} {color[2]:.6f}")

The script above returns the following RGB colors:

0.000000 0.000000 0.500000
0.000000 0.000000 0.731729
0.000000 0.000000 0.963458
0.000000 0.127451 1.000000
0.000000 0.331373 1.000000
0.000000 0.550980 1.000000
0.000000 0.754902 1.000000
0.060089 0.974510 0.907653
0.224541 1.000000 0.743201
0.401645 1.000000 0.566097
0.566097 1.000000 0.401645
0.743201 1.000000 0.224541
0.907653 1.000000 0.060089
1.000000 0.828613 0.000000
1.000000 0.639797 0.000000
1.000000 0.436456 0.000000
1.000000 0.247640 0.000000
0.963458 0.044299 0.000000
0.731729 0.000000 0.000000
0.500000 0.000000 0.000000

Save the RGB colors above as a text file, for example, jet_matplotlib.txt.

Then, open FSLeyes and load the custom colormap through the overlay display panel. The colormap will be saved to $HOME/.fsleyes/colourmaps directory.

Sources:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment