def plot_csd(csd, info=None, mode='csd', colorbar=True, cmap=None, n_cols=None, show=True):
    "Plot CSD matrices.\n\n    A sub-plot is created for each frequency. If an info object is passed to\n    the function, different channel types are plotted in different figures.\n\n    Parameters\n    ----------\n    csd : instance of CrossSpectralDensity\n        The CSD matrix to plot.\n    info : instance of Info | None\n        To split the figure by channel-type, provide the measurement info.\n        By default, the CSD matrix is plotted as a whole.\n    mode : 'csd' | 'coh'\n        Whether to plot the cross-spectral density ('csd', the default), or\n        the coherence ('coh') between the channels.\n    colorbar : bool\n        Whether to show a colorbar. Defaults to ``True``.\n    cmap : str | None\n        The matplotlib colormap to use. Defaults to None, which means the\n        colormap will default to matplotlib's default.\n    n_cols : int | None\n        CSD matrices are plotted in a grid. This parameter controls how\n        many matrix to plot side by side before starting a new row. By\n        default, a number will be chosen to make the grid as square as\n        possible.\n    show : bool\n        Whether to show the figure. Defaults to ``True``.\n\n    Returns\n    -------\n    fig : list of Figure\n        The figures created by this function.\n    "
    import matplotlib.pyplot as plt
    if (mode not in ['csd', 'coh']):
        raise ValueError('"mode" should be either "csd" or "coh".')
    if (info is not None):
        info_ch_names = info['ch_names']
        sel_eeg = pick_types(info, meg=False, eeg=True, ref_meg=False, exclude=[])
        sel_mag = pick_types(info, meg='mag', eeg=False, ref_meg=False, exclude=[])
        sel_grad = pick_types(info, meg='grad', eeg=False, ref_meg=False, exclude=[])
        idx_eeg = [csd.ch_names.index(info_ch_names[c]) for c in sel_eeg if (info_ch_names[c] in csd.ch_names)]
        idx_mag = [csd.ch_names.index(info_ch_names[c]) for c in sel_mag if (info_ch_names[c] in csd.ch_names)]
        idx_grad = [csd.ch_names.index(info_ch_names[c]) for c in sel_grad if (info_ch_names[c] in csd.ch_names)]
        indices = [idx_eeg, idx_mag, idx_grad]
        titles = ['EEG', 'Magnetometers', 'Gradiometers']
        if (mode == 'csd'):
            units = dict(eeg='µV²', grad='fT²/cm²', mag='fT²')
            scalings = dict(eeg=1000000000000.0, grad=1e+26, mag=1e+30)
    else:
        indices = [np.arange(len(csd.ch_names))]
        if (mode == 'csd'):
            titles = ['Cross-spectral density']
            units = dict()
            scalings = dict()
        elif (mode == 'coh'):
            titles = ['Coherence']
    n_freqs = len(csd.frequencies)
    if (n_cols is None):
        n_cols = int(np.ceil(np.sqrt(n_freqs)))
    n_rows = int(np.ceil((n_freqs / float(n_cols))))
    figs = []
    for (ind, title, ch_type) in zip(indices, titles, ['eeg', 'mag', 'grad']):
        if (len(ind) == 0):
            continue
        (fig, axes) = plt.subplots(n_rows, n_cols, squeeze=False, figsize=(((2 * n_cols) + 1), (2.2 * n_rows)))
        csd_mats = []
        for i in range(len(csd.frequencies)):
            cm = csd.get_data(index=i)[ind][:, ind]
            if (mode == 'csd'):
                cm = (np.abs(cm) * scalings.get(ch_type, 1))
            elif (mode == 'coh'):
                psd = np.diag(cm).real
                cm = (((np.abs(cm) ** 2) / psd[np.newaxis, :]) / psd[:, np.newaxis])
            csd_mats.append(cm)
        vmax = np.max(csd_mats)
        for (i, (freq, mat)) in enumerate(zip(csd.frequencies, csd_mats)):
            ax = axes[(i // n_cols)][(i % n_cols)]
            im = ax.imshow(mat, interpolation='nearest', cmap=cmap, vmin=0, vmax=vmax)
            ax.set_xticks([])
            ax.set_yticks([])
            if csd._is_sum:
                ax.set_title(('%.1f-%.1f Hz.' % (np.min(freq), np.max(freq))))
            else:
                ax.set_title(('%.1f Hz.' % freq))
        plt.suptitle(title)
        plt.subplots_adjust(top=0.8)
        if colorbar:
            cb = plt.colorbar(im, ax=[a for ax_ in axes for a in ax_])
            if (mode == 'csd'):
                label = u'CSD'
                if (ch_type in units):
                    label += (u' (%s)' % units[ch_type])
                cb.set_label(label)
            elif (mode == 'coh'):
                cb.set_label('Coherence')
        figs.append(fig)
    plt_show(show)
    return figs
Plot CSD matrices.

A sub-plot is created for each frequency. If an info object is passed to
the function, different channel types are plotted in different figures.

Parameters
----------
csd : instance of CrossSpectralDensity
    The CSD matrix to plot.
info : instance of Info | None
    To split the figure by channel-type, provide the measurement info.
    By default, the CSD matrix is plotted as a whole.
mode : 'csd' | 'coh'
    Whether to plot the cross-spectral density ('csd', the default), or
    the coherence ('coh') between the channels.
colorbar : bool
    Whether to show a colorbar. Defaults to ``True``.
cmap : str | None
    The matplotlib colormap to use. Defaults to None, which means the
    colormap will default to matplotlib's default.
n_cols : int | None
    CSD matrices are plotted in a grid. This parameter controls how
    many matrix to plot side by side before starting a new row. By
    default, a number will be chosen to make the grid as square as
    possible.
show : bool
    Whether to show the figure. Defaults to ``True``.

Returns
-------
fig : list of Figure
    The figures created by this function.
def plot_csd(csd, info=None, mode='csd', colorbar=True, cmap=None, n_cols=None, show=True):
    "Plot CSD matrices.\n\n    A sub-plot is created for each frequency. If an info object is passed to\n    the function, different channel types are plotted in different figures.\n\n    Parameters\n    ----------\n    csd : instance of CrossSpectralDensity\n        The CSD matrix to plot.\n    info : instance of Info | None\n        To split the figure by channel-type, provide the measurement info.\n        By default, the CSD matrix is plotted as a whole.\n    mode : 'csd' | 'coh'\n        Whether to plot the cross-spectral density ('csd', the default), or\n        the coherence ('coh') between the channels.\n    colorbar : bool\n        Whether to show a colorbar. Defaults to ``True``.\n    cmap : str | None\n        The matplotlib colormap to use. Defaults to None, which means the\n        colormap will default to matplotlib's default.\n    n_cols : int | None\n        CSD matrices are plotted in a grid. This parameter controls how\n        many matrix to plot side by side before starting a new row. By\n        default, a number will be chosen to make the grid as square as\n        possible.\n    show : bool\n        Whether to show the figure. Defaults to ``True``.\n\n    Returns\n    -------\n    fig : list of Figure\n        The figures created by this function.\n    "
    import matplotlib.pyplot as plt
    if (mode not in ['csd', 'coh']):
        raise ValueError('"mode" should be either "csd" or "coh".')
    if (info is not None):
        info_ch_names = info['ch_names']
        sel_eeg = pick_types(info, meg=False, eeg=True, ref_meg=False, exclude=[])
        sel_mag = pick_types(info, meg='mag', eeg=False, ref_meg=False, exclude=[])
        sel_grad = pick_types(info, meg='grad', eeg=False, ref_meg=False, exclude=[])
        idx_eeg = [csd.ch_names.index(info_ch_names[c]) for c in sel_eeg if (info_ch_names[c] in csd.ch_names)]
        idx_mag = [csd.ch_names.index(info_ch_names[c]) for c in sel_mag if (info_ch_names[c] in csd.ch_names)]
        idx_grad = [csd.ch_names.index(info_ch_names[c]) for c in sel_grad if (info_ch_names[c] in csd.ch_names)]
        indices = [idx_eeg, idx_mag, idx_grad]
        titles = ['EEG', 'Magnetometers', 'Gradiometers']
        if (mode == 'csd'):
            units = dict(eeg='µV²', grad='fT²/cm²', mag='fT²')
            scalings = dict(eeg=1000000000000.0, grad=1e+26, mag=1e+30)
    else:
        indices = [np.arange(len(csd.ch_names))]
        if (mode == 'csd'):
            titles = ['Cross-spectral density']
            units = dict()
            scalings = dict()
        elif (mode == 'coh'):
            titles = ['Coherence']
    n_freqs = len(csd.frequencies)
    if (n_cols is None):
        n_cols = int(np.ceil(np.sqrt(n_freqs)))
    n_rows = int(np.ceil((n_freqs / float(n_cols))))
    figs = []
    for (ind, title, ch_type) in zip(indices, titles, ['eeg', 'mag', 'grad']):
        if (len(ind) == 0):
            continue
        (fig, axes) = plt.subplots(n_rows, n_cols, squeeze=False, figsize=(((2 * n_cols) + 1), (2.2 * n_rows)))
        csd_mats = []
        for i in range(len(csd.frequencies)):
            cm = csd.get_data(index=i)[ind][:, ind]
            if (mode == 'csd'):
                cm = (np.abs(cm) * scalings.get(ch_type, 1))
            elif (mode == 'coh'):
                psd = np.diag(cm).real
                cm = (((np.abs(cm) ** 2) / psd[np.newaxis, :]) / psd[:, np.newaxis])
            csd_mats.append(cm)
        vmax = np.max(csd_mats)
        for (i, (freq, mat)) in enumerate(zip(csd.frequencies, csd_mats)):
            ax = axes[(i // n_cols)][(i % n_cols)]
            im = ax.imshow(mat, interpolation='nearest', cmap=cmap, vmin=0, vmax=vmax)
            ax.set_xticks([])
            ax.set_yticks([])
            if csd._is_sum:
                ax.set_title(('%.1f-%.1f Hz.' % (np.min(freq), np.max(freq))))
            else:
                ax.set_title(('%.1f Hz.' % freq))
        plt.suptitle(title)
        plt.subplots_adjust(top=0.8)
        if colorbar:
            cb = plt.colorbar(im, ax=[a for ax_ in axes for a in ax_])
            if (mode == 'csd'):
                label = u'CSD'
                if (ch_type in units):
                    label += (u' (%s)' % units[ch_type])
                cb.set_label(label)
            elif (mode == 'coh'):
                cb.set_label('Coherence')
        figs.append(fig)
    plt_show(show)
    return figs