query regarding plot_topomap

  • MNE version: 1.3.1
  • operating system: e.g. macOS 12

info = create_info(ch_names, sfreq, ch_types=‘hbo’)

channel_positions = channel_positions

mean_hbo_timeavg = np.mean(mean_hbo, axis=1)

fig, ax = plt.subplots(figsize=(8, 8))

plot_topomap(mean_hbo_timeavg, channel_positions, cmap=‘plasma’, sensors=True, show=True, outlines=‘head’, extrapolate = ‘local’,res=128,image_interp=‘cubic’, vlim=(-1,1) ,names=actual_ch_names,sphere = (0, 0, 0, 0.095) , axes=ax)

plt.show()

I get the plot as:


As you can see it doesn’t show the entire head.

when I change extrapolate to ‘head’ I get:

But I want to use extrapolate as ‘local’ with the contour of the head with ears and nose like topographic plots in Preprocessing functional near-infrared spectroscopy (fNIRS) data — MNE 1.3.1 documentation. Also is it possible to change the size and color of the channel names ?

Any help would be appreciated.

I think the dot in the middle of your second plot is the head. This can happen for example when your channel positions are saved in cm but read as meters (the default in mne). In new mne versions this should raise a warning.
How did you read the file or the position of the channels?

(if the channel positions are indeed on wrong units, then fixing this should give you correct plot in both cases)

3 Likes

@calebshibu Perhaps this post will help you to solve the issue. If its a re-scaling issue, you can use the code snippet provided there.

best,
Dip

1 Like

Thank you. That fixed my issue. Also how can I change the color and size of the channel names in topographic plots?

Yup you’re right. Diving by 1000 fixed my issue.

Also how can I change the color and size of the channel names in topographic plots?

the plot_topomap function returns a tuple of a matplotlib AxesImage and a matplotlib ContourSet. You can access the sensor labels like this:

img, cont = plot_topomap(...)
sensor_labels = img.axes.texts

…which is a list-like iterable, so you can do things like

for label in sensor_labels:
    label.set_color(...)
    label.set_size(...)

etc

watch out that img.axes.texts will (I think) include any annotations you make, and maybe also the plot title? So either update the sensor formatting before adding those, or add some logic inside the for-loop to only alter the sensor names.

1 Like

I tried doing this:

image, contours = plot_topomap(mean_hbo_timeavg, channel_positions/1000, cmap='bwr', sensors=True, show=True, outlines='head', extrapolate = 'local',res=128,image_interp='cubic',  vlim=(-1,1) ,names=actual_ch_names,sphere =  (0, 0, 0, 0.095) , axes=ax, contours=0)
# fig.savefig('topomap.png', dpi=300)
sensor_labels = image.axes.texts

# Set the color and size of the sensor labels
for label in sensor_labels:
    # if label.get_text().startswith("EEG") or label.get_text().startswith("MEG"):
        label.set_color('white')
        label.set_size(10)
# Show the plot
plt.show()

But it doesn’t change the text size and color in the plots.

Also I want to see the color scale as well so I added colorbar=True but I end up with this error:
Cell In[49], line 56 53 mean_hbo_timeavg = np.mean(mean_hbo, axis=1) 55 fig, ax = plt.subplots(figsize=(8, 8)) ---> 56 image, contours = plot_topomap(mean_hbo_timeavg, channel_positions[/](https://file+.vscode-resource.vscode-cdn.net/)1000, cmap='bwr', sensors=True, show=True, outlines='head', extrapolate = 'local',res=128,image_interp='cubic', vlim=(-1,1) ,names=actual_ch_names,sphere = (0, 0, 0, 0.095) , axes=ax, contours=0, colorbar=True) 57 # fig.savefig('topomap.png', dpi=300) 58 sensor_labels = image.axes.texts TypeError: plot_topomap() got an unexpected keyword argument 'colorbar'

this approach definitely worked for me:

img, cont = mne.viz.plot_topomap(...)
for txt in img.axes.texts:
    txt.set_color('cyan')

Try adding plt.ion() before you create the figure? Or if you’re doing this in a Jupyter Notebook, make sure you alter the texts in the same cell that the plot was created in.

That function doesn’t have a colorbar parameter yet, that is a known issue: Add `colorbar` parameter to `mne.viz.plot_topomap()`? · Issue #11522 · mne-tools/mne-python · GitHub Meanwhile you can use matplotlib methods to add a colorbar: Colorbar — Matplotlib 3.8.2 documentation

AFAIK the person assigned to work on adding colorbars to this function hasn’t actually implemented anything yet so if you were up for it you could submit a pull request to add colorbar support.