color & spatial_colors totally NOT WORK

I was recently trying to make my visualizations look better and I searched the forums and saw some suggestions.

I listened to a comment https://mne.discourse.group/t/channels-colors-in-psd-plot/6572/3, claiming that color=LineCollection() can be used to modify the color.

But after I wrote my code according to https://matplotlib.org/stable/gallery/shapes_and_collections/line_collection.html , I found that all my color configurations became this newly created LineCollection(), which can never be modified.

No matter if I restart Jupyter or modify other color settings or set color/spatial_colors, I cannot undo this modification. All the pictures I made with MNE have turned into very ugly colors. And it cannot be modified!

My code is as follows:

from matplotlib.collections import LineCollection
import numpy as np

N = 118
x = np.arange(N)
ys = [x + i for i in x]  # Many sets of y to plot vs. x
segs = [np.column_stack([x, y]) for y in ys]

fig, ax = plt.subplots()
ax.set_xlim(np.min(x), np.max(x))
ax.set_ylim(np.min(ys), np.max(ys))

line_segments = LineCollection(segs, array=x,
                               linewidths=(0.5, 1, 1.5, 2),
                               linestyles='solid')
ax.add_collection(line_segments)
axcb = fig.colorbar(line_segments)
axcb.set_label('Line Number')
ax.set_title('Line Collection with mapped colors')
plt.sci(line_segments)  # This allows interactive changing of the colormap.
plt.show()

raw.compute_psd().plot(color=line_segments, spatial_colors=True)

I tried reinstalling MNE, matplotlib and even reinstalling the entire virtual environment, but there is still no way to modify the color configuration.

Sincerely looking for a solution.

Hello @worsh, welcome to the forum!

I’m not sure if this will help, but you could try deleting the MNE configuration file. This page describes how to locate it:

https://mne.tools/stable/auto_tutorials/intro/50_configure_mne.html#where-configurations-are-stored

Good luck,
Richard

Thanks for your answer, but it doesn’t seem to work after I tried it. I tried setting up a new virtual environment, but it seems the effect is across the virtual environment, and I’m still trying. :smiling_face_with_tear:

I think you’ve misunderstood the advice. You are passing a LineCollection object to the color parameter of the function (which won’t work), and also passing spatial_colors=True (which will override whatever you pass to color) — note that the docstring says

color : str | tuple
A matplotlib-compatible color to use. Has no effect when spatial_colors=True.

The advice in the other thread was to not use Spectrum.plot() at all and instead use matplotlib to directly plot the channel traces. For example:

import matplotlib as mpl
import matplotlib.pyplot as plt
import mne


sample_data_folder = mne.datasets.sample.data_path()
sample_data_raw_file = (sample_data_folder / 'MEG' / 'sample' /
                        'sample_audvis_raw.fif')
raw = mne.io.read_raw_fif(sample_data_raw_file, verbose=False, preload=False)
spect = raw.compute_psd(picks="eeg", exclude="bads")

cmap = mpl.colormaps['cividis']
n_ch = len(spect.ch_names)
custom_colors = [cmap(n/n_ch) for n in range(n_ch)]

fig, ax = plt.subplots()

for channel, color in zip(spect.get_data(), custom_colors):
    ax.loglog(spect.freqs, channel, color=color)

Figure_1

This is the first of the two suggested methods (zip channels and colors together and plot each trace in a for-loop). The other option (using a LineCollection) is a bit more complicated but gives basically the same result.

Thank you, I seem to understand why. I reproduced this problem on other people’s computers, and the cause of the problem was an incorrect data type. Because the electrode data I used is missing a dimension, the spatial color is inconsistent with what I imagined. But in fact, the spatial color is displayed correctly.

1 Like