Using annotations for purposes other than marking artifacts: A question about coloring

  • MNE version: e.g. 0.24.0
  • Mac OS 11.6.1

I’m using annotations for purposes other than marking artifacts. Specifically, I’ve partitioned
My data into equal-size time windows and am grouping the time windows into clusters based upon a certain connectivity characteristic. I’d like to assign the same color to windows belonging to the same cluster. An example illustration for a 10 second segment broken into 20 time windows is shown below, where there are five distinct colors corresponding to the five clusters. The Annotations instance was formed using equally placed onsets, fixed duration of .5 sec, and descriptions corresponding to the cluster numbers, e.g. ‘1’, ‘2’,…,’5’.

Am I correct that the function _get_color_list within mne/viz/ is where colors are determined for annotation purposes?

The following four lines are in this function between lines 1515 and 1522:

from matplotlib import rcParams
color_cycle = rcParams.get('axes.prop_cycle')
colors = color_cycle.by_key()['color']

The output is

[’#1f77b4’, ‘#ff7f0e’, ‘#2ca02c’, ‘#d62728’, ‘#9467bd’, ‘#8c564b’, ‘#e377c2’, ‘#7f7f7f’, ‘#bcbd22’, ‘#17becf’]

Does this mean that in the default configuration, the number of color possibilities for annotations is 10?

If so, could I enlarge the list of colors somehow to highlight a larger number of clusters, e.g. 20 colors in the case I have 20 clusters?

yes. Matplotlib default color cycle has length ten. Incidentally, those colors are accessible as 'C0', 'C1', … 'C9' in Matplotib functions, and hence in most (probably not all?) places in MNE-Python where we allow users to specify a color.

I think your easiest approach is to change the matplotlib color cycle to be longer, see Styling with cycler — Matplotlib 3.5.1 documentation (see also this SO answer: python - use matplotlib color map for color cycle - Stack Overflow)

Nicer would be to somehow allow to pass a custom list of annotations colors in our API; if you have an idea of how you would do that please consider making a PR!

Wonderful !!

I went with your easiest approach and everything looks much better.