|
11 | 11 | import numpy as np
|
12 | 12 |
|
13 | 13 | import matplotlib.cbook as cbook
|
14 |
| -import matplotlib.cm as cm |
15 |
| -from matplotlib.collections import LineCollection |
16 | 14 |
|
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 | +) |
18 | 23 |
|
19 | 24 | # Load the MRI data (256x256 16-bit integers)
|
20 | 25 | with cbook.get_sample_data('s1045.ima.gz') as dfile:
|
21 | 26 | im = np.frombuffer(dfile.read(), np.uint16).reshape((256, 256))
|
22 | 27 |
|
23 | 28 | # 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') |
27 | 31 |
|
28 | 32 | # Plot the histogram of MRI intensity
|
29 |
| -ax1 = fig.add_subplot(2, 2, 2) |
30 | 33 | 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() |
34 | 38 |
|
35 | 39 | # Load the EEG data
|
36 | 40 | n_samples, n_rows = 800, 4
|
|
39 | 43 | t = 10 * np.arange(n_samples) / n_samples
|
40 | 44 |
|
41 | 45 | # 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']) |
52 | 51 |
|
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") |
57 | 54 |
|
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() |
70 | 55 | plt.show()
|
0 commit comments