Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1e0c0bd

Browse files
committed
Simplify MRI with EEG example
1 parent ee723d9 commit 1e0c0bd

File tree

1 file changed

+21
-36
lines changed

1 file changed

+21
-36
lines changed

galleries/examples/specialty_plots/mri_with_eeg.py

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,30 @@
1111
import numpy as np
1212

1313
import matplotlib.cbook as cbook
14-
import matplotlib.cm as cm
15-
from matplotlib.collections import LineCollection
1614

17-
fig = plt.figure("MRI_with_EEG")
15+
fig, axd = plt.subplot_mosaic(
16+
[["image", "density"],
17+
["EEG", "EEG"]],
18+
layout="constrained",
19+
# "image" will contain a square image. We fine-tune the width so that
20+
# there is no excess horizontal or vertical margin around the image.
21+
width_ratios=[1.05, 2],
22+
)
1823

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

2328
# Plot the MRI image
24-
ax0 = fig.add_subplot(2, 2, 1)
25-
ax0.imshow(im, cmap=cm.gray)
26-
ax0.axis('off')
29+
axd["image"].imshow(im, cmap="gray")
30+
axd["image"].axis('off')
2731

2832
# Plot the histogram of MRI intensity
29-
ax1 = fig.add_subplot(2, 2, 2)
3033
im = im[im.nonzero()] # Ignore the background
31-
ax1.hist(im, bins=np.arange(0, 2**16+1, 512))
32-
ax1.set(xlabel='Intensity (a.u.)', ylabel='MRI density', yticks=[])
33-
ax1.minorticks_on()
34+
axd["density"].hist(im, bins=np.arange(0, 2**16+1, 512))
35+
axd["density"].set(xlabel='Intensity (a.u.)', xlim=(0, 2**16),
36+
ylabel='MRI density', yticks=[])
37+
axd["density"].minorticks_on()
3438

3539
# Load the EEG data
3640
n_samples, n_rows = 800, 4
@@ -39,32 +43,13 @@
3943
t = 10 * np.arange(n_samples) / n_samples
4044

4145
# Plot the EEG
42-
ticklocs = []
43-
ax2 = fig.add_subplot(2, 1, 2)
44-
ax2.set_xlim(0, 10)
45-
ax2.set_xticks(np.arange(10))
46-
dmin = data.min()
47-
dmax = data.max()
48-
dr = (dmax - dmin) * 0.7 # Crowd them a bit.
49-
y0 = dmin
50-
y1 = (n_rows - 1) * dr + dmax
51-
ax2.set_ylim(y0, y1)
46+
axd["EEG"].set_xlabel('Time (s)')
47+
axd["EEG"].set_xlim(0, 10)
48+
dy = (data.min() - data.max()) * 0.7 # Crowd them a bit.
49+
axd["EEG"].set_ylim(-dy, n_rows * dy)
50+
axd["EEG"].set_yticks([0, dy, 2*dy, 3*dy], labels=['PG3', 'PG5', 'PG7', 'PG9'])
5251

53-
segs = []
54-
for i in range(n_rows):
55-
segs.append(np.column_stack((t, data[:, i])))
56-
ticklocs.append(i * dr)
52+
for i, data_col in enumerate(data.T):
53+
axd["EEG"].plot(t, data_col + i*dy, color="C0")
5754

58-
offsets = np.zeros((n_rows, 2), dtype=float)
59-
offsets[:, 1] = ticklocs
60-
61-
lines = LineCollection(segs, offsets=offsets, offset_transform=None)
62-
ax2.add_collection(lines)
63-
64-
# Set the yticks to use axes coordinates on the y-axis
65-
ax2.set_yticks(ticklocs, labels=['PG3', 'PG5', 'PG7', 'PG9'])
66-
67-
ax2.set_xlabel('Time (s)')
68-
69-
plt.tight_layout()
7055
plt.show()

0 commit comments

Comments
 (0)