{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Plot custom topographies for MEG sensors\n\n\nThis example exposes the `iter_topography` function that makes it\nvery easy to generate custom sensor topography plots.\nHere we will plot the power spectrum of each channel on a topographic\nlayout.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Author: Denis A. Engemann \n#\n# License: BSD (3-clause)\n\n\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nimport mne\nfrom mne.viz import iter_topography\nfrom mne import io\nfrom mne.time_frequency import psd_welch\nfrom mne.datasets import sample\n\nprint(__doc__)\n\ndata_path = sample.data_path()\nraw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif'\n\nraw = io.read_raw_fif(raw_fname, preload=True)\nraw.filter(1, 20, fir_design='firwin')\n\npicks = mne.pick_types(raw.info, meg=True, exclude=[])\ntmin, tmax = 0, 120 # use the first 120s of data\nfmin, fmax = 2, 20 # look at frequencies between 2 and 20Hz\nn_fft = 2048 # the FFT size (n_fft). Ideally a power of 2\npsds, freqs = psd_welch(raw, picks=picks, tmin=tmin, tmax=tmax,\n fmin=fmin, fmax=fmax)\npsds = 20 * np.log10(psds) # scale to dB\n\n\ndef my_callback(ax, ch_idx):\n \"\"\"\n This block of code is executed once you click on one of the channel axes\n in the plot. To work with the viz internals, this function should only take\n two parameters, the axis and the channel or data index.\n \"\"\"\n ax.plot(freqs, psds[ch_idx], color='red')\n ax.set_xlabel = 'Frequency (Hz)'\n ax.set_ylabel = 'Power (dB)'\n\n\nfor ax, idx in iter_topography(raw.info,\n fig_facecolor='white',\n axis_facecolor='white',\n axis_spinecolor='white',\n on_pick=my_callback):\n ax.plot(psds[idx], color='red')\n\nplt.gcf().suptitle('Power spectral densities')\nplt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.4" } }, "nbformat": 4, "nbformat_minor": 0 }