8000 DOC: switch pylab example `mri_with_eeg.py` to OO interface + cosmetick fixes by afvincent · Pull Request #7192 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

DOC: switch pylab example mri_with_eeg.py to OO interface + cosmetick fixes #7192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 46 additions & 44 deletions examples/pylab_examples/mri_with_eeg.py
Original file line number Diff line number Diff line change
@@ -1,77 +1,79 @@
"""
This now uses the imshow command instead of pcolor which *is much
faster*
"""Displays a set of subplots with an MRI image, its intensity histogram and
some EEG traces.

NB: ones uses the imshow command instead of pcolor which *is much faster*.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete this comment line. Even mentioning pcolor here is confusing. I suspect the line dates back to the very earliest days, when imshow was first added to mpl.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You were right (see 6721163). I have removed it.

"""

from __future__ import division, print_function

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

from matplotlib.pyplot import *
from matplotlib.collections import LineCollection
import matplotlib.cbook as cbook
# I use if 1 to break up the different regions of code visually
from matplotlib.ticker import MultipleLocator

if 1: # load the data
# data are 256x256 16 bit integers
# NB: one uses "if 1:" to break up the different regions of code visually
fig = plt.figure("MRI_with_EEG")

if 1: # Load the data
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really dislike this syntay. Can we get rid of this? It is using python code for documentation purposes.
Having comments should be enough to break the different regions of the code visually.

Copy link
Contributor Author
@afvincent afvincent Sep 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed a new commit (d06e572) that get rid of this idiom.

# Data are 256x256 16 bit integers
dfile = cbook.get_sample_data('s1045.ima.gz')
im = np.fromstring(dfile.read(), np.uint16).astype(float)
im.shape = 256, 256
im.shape = (256, 256)

if 1: # plot the MRI in pcolor
subplot(221)
imshow(im, cmap=cm.gray)
axis('off')
if 1: # Plot the MRI image
ax0 = fig.add_subplot(2, 2, 1)
ax0.imshow(im, cmap=cm.gray)
ax0.axis('off')

if 1: # plot the histogram of MRI intensity
subplot(222)
if 1: # 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.0**15) # normalize
hist(im, 100)
xticks([-1, -.5, 0, .5, 1])
yticks([])
xlabel('intensity')
ylabel('MRI density')

if 1: # plot the EEG
# load the data
im = im[np.nonzero(im)] # Ignore the background
im = im / (2**15) # Normalize
ax1.hist(im, 100)
ax1.xaxis.set_major_locator(MultipleLocator(0.5))
ax1.set_yticks([])
ax1.set_xlabel('Intensity')
ax1.set_ylabel('MRI density')

if 1: # Plot the EEG
# Load the data
numSamples, numRows = 800, 4
eegfile = cbook.get_sample_data('eeg.dat', asfileobj=False)
print('loading eeg %s' % eegfile)
print('Loading EEG %s' % eegfile)
data = np.fromstring(open(eegfile, 'rb').read(), float)
data.shape = numSamples, numRows
t = 10.0 * np.arange(numSamples, dtype=float)/numSamples
data.shape = (numSamples, numRows)
t = 10.0 * np.arange(numSamples) / numSamples
ticklocs = []
ax = subplot(212)
xlim(0, 10)
xticks(np.arange(10))
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.
dr = (dmax - dmin) * 0.7 # Crowd them a bit.
y0 = dmin
y1 = (numRows - 1) * dr + dmax
ylim(y0, y1)
ax2.set_ylim(y0, y1)

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

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

lines = LineCollection(segs, offsets=offsets,
transOffset=None,
)

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

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

xlabel('time (s)')
ax2.set_xlabel('Time (s)')

show()
plt.tight_layout()
plt.show()
0