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.
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.
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)
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.