Key Error when attempting to savefig of plot_anat with add_markers added

It seems a simple thing, but maybe I’m missing something.
I’m trying to plot brain slices with markers for my ROIs.
I’ve tried different variations of the following, to no avail.

The errors seem to be related to a KeyError, and perhaps Axes as well.

cont_name is a folder name defined elsewhere.
mni_img is loaded from $FSLDIR/data/standard/MNI152_T1_1mm.nii.gz

    coords = all_peaks[['x','y','z']].to_numpy()
    z_coords = sorted(coords[:,2])
    fig = plt.figure(figsize=(8, 6), dpi=600, facecolor='white')
    display = plot_anat(mni_img, figure=fig, display_mode='z', cut_coords=z_coords)
    display.add_markers(coords, figure=fig)
    display.savefig(f'/output/2ndLevel/{cont_name}/task-ao/cluster_z_slices.png')

Here’s a gist of the errors:

Thanks for any suggestions!

Hi,
My impression is that cut_coords should be lists of 3-dimensional tuples.
Let me know if this solves your problem.
Best,
Bertrand

That’s true for the ‘ortho’ view, but x, y, z slices use a single value per slice.

cut_coordsNone, a tuple of float, or int, optional
The MNI coordinates of the point where the cut is performed.
If display_mode is ‘ortho’ or ‘tiled’, this should be a 3-tuple: (x, y, z)
For display_mode == 'x', ‘y’, or ‘z’, then these are the coordinates of each cut in the corresponding direction.
If None is given, the cuts are calculated automatically.
If display_mode is ‘mosaic’, and the number of cuts is the same for all directions, cut_coords can be specified as an integer. It can also be a length 3 tuple specifying the number of cuts for every direction if these are different.
Note If display_mode is ‘x’, ‘y’ or ‘z’, cut_coords can be an integer, in which case it specifies the number of cuts to perform.

The traceback looks similar to the one from this issue: plotting crash in single axis mode but not ortho · Issue #1070 · nilearn/nilearn · GitHub. That issue was originally closed because it wasn’t reproducible. If your code consistently raises that exception, then it might be useful for the Nilearn issue.

I think it is indeed.

Thanks @bthirion and @tsalo .
I’ll create an issue on GitHub.