
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/specialty_plots/mri_with_eeg.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        Click :ref:`here <sphx_glr_download_gallery_specialty_plots_mri_with_eeg.py>`
        to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_specialty_plots_mri_with_eeg.py:


============
MRI With EEG
============

Displays a set of subplots with an MRI image, its intensity
histogram and some EEG traces.

.. GENERATED FROM PYTHON SOURCE LINES 9-79



.. image:: /gallery/specialty_plots/images/sphx_glr_mri_with_eeg_001.png
    :alt: mri with eeg
    :class: sphx-glr-single-img





.. code-block:: default


    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.cbook as cbook
    import matplotlib.cm as cm

    from matplotlib.collections import LineCollection
    from matplotlib.ticker import MultipleLocator

    fig = plt.figure("MRI_with_EEG")

    # Load the MRI data (256x256 16 bit integers)
    with cbook.get_sample_data('s1045.ima.gz') as dfile:
        im = np.frombuffer(dfile.read(), np.uint16).reshape((256, 256))

    # Plot the MRI image
    ax0 = fig.add_subplot(2, 2, 1)
    ax0.imshow(im, cmap=cm.gray)
    ax0.axis('off')

    # Plot the histogram of MRI intensity
    ax1 = fig.add_subplot(2, 2, 2)
    im = np.ravel(im)
    im = im[np.nonzero(im)]  # Ignore the background
    im = im / (2**16 - 1)  # Normalize
    ax1.hist(im, bins=100)
    ax1.xaxis.set_major_locator(MultipleLocator(0.4))
    ax1.minorticks_on()
    ax1.set_yticks([])
    ax1.set_xlabel('Intensity (a.u.)')
    ax1.set_ylabel('MRI density')

    # Load the EEG data
    n_samples, n_rows = 800, 4
    with cbook.get_sample_data('eeg.dat') as eegfile:
        data = np.fromfile(eegfile, dtype=float).reshape((n_samples, n_rows))
    t = 10 * np.arange(n_samples) / n_samples

    # Plot the EEG
    ticklocs = []
    ax2 = fig.add_subplot(2, 1, 2)
    ax2.set_xlim(0, 10)
    ax2.set_xticks(np.arange(10))
    dmin = data.min()
    dmax = data.max()
    dr = (dmax - dmin) * 0.7  # Crowd them a bit.
    y0 = dmin
    y1 = (n_rows - 1) * dr + dmax
    ax2.set_ylim(y0, y1)

    segs = []
    for i in range(n_rows):
        segs.append(np.column_stack((t, data[:, i])))
        ticklocs.append(i * dr)

    offsets = np.zeros((n_rows, 2), dtype=float)
    offsets[:, 1] = ticklocs

    lines = LineCollection(segs, offsets=offsets, transOffset=None)
    ax2.add_collection(lines)

    # Set the yticks to use axes coordinates on the y axis
    ax2.set_yticks(ticklocs)
    ax2.set_yticklabels(['PG3', 'PG5', 'PG7', 'PG9'])

    ax2.set_xlabel('Time (s)')


    plt.tight_layout()
    plt.show()


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** ( 0 minutes  1.150 seconds)


.. _sphx_glr_download_gallery_specialty_plots_mri_with_eeg.py:


.. only :: html

 .. container:: sphx-glr-footer
    :class: sphx-glr-footer-example



  .. container:: sphx-glr-download sphx-glr-download-python

     :download:`Download Python source code: mri_with_eeg.py <mri_with_eeg.py>`



  .. container:: sphx-glr-download sphx-glr-download-jupyter

     :download:`Download Jupyter notebook: mri_with_eeg.ipynb <mri_with_eeg.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    Keywords: matplotlib code example, codex, python plot, pyplot
    `Gallery generated by Sphinx-Gallery
    <https://sphinx-gallery.readthedocs.io>`_
