{ "nbformat_minor": 0, "nbformat": 4, "cells": [ { "execution_count": null, "cell_type": "code", "source": [ "%matplotlib inline" ], "outputs": [], "metadata": { "collapsed": false } }, { "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\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "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)\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\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()" ], "outputs": [], "metadata": { "collapsed": false } } ], "metadata": { "kernelspec": { "display_name": "Python 2", "name": "python2", "language": "python" }, "language_info": { "mimetype": "text/x-python", "nbconvert_exporter": "python", "name": "python", "file_extension": ".py", "version": "2.7.13", "pygments_lexer": "ipython2", "codemirror_mode": { "version": 2, "name": "ipython" } } } }