MRI与脑电图

显示一组带有MRI图像的子图,其强度直方图和一些EEG轨迹。

MRI与脑电图示例

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import matplotlib.cbook as cbook
  4. import matplotlib.cm as cm
  5. from matplotlib.collections import LineCollection
  6. from matplotlib.ticker import MultipleLocator
  7. fig = plt.figure("MRI_with_EEG")
  8. # Load the MRI data (256x256 16 bit integers)
  9. with cbook.get_sample_data('s1045.ima.gz') as dfile:
  10. im = np.fromstring(dfile.read(), np.uint16).reshape((256, 256))
  11. # Plot the MRI image
  12. ax0 = fig.add_subplot(2, 2, 1)
  13. ax0.imshow(im, cmap=cm.gray)
  14. ax0.axis('off')
  15. # Plot the histogram of MRI intensity
  16. ax1 = fig.add_subplot(2, 2, 2)
  17. im = np.ravel(im)
  18. im = im[np.nonzero(im)] # Ignore the background
  19. im = im / (2**16 - 1) # Normalize
  20. ax1.hist(im, bins=100)
  21. ax1.xaxis.set_major_locator(MultipleLocator(0.4))
  22. ax1.minorticks_on()
  23. ax1.set_yticks([])
  24. ax1.set_xlabel('Intensity (a.u.)')
  25. ax1.set_ylabel('MRI density')
  26. # Load the EEG data
  27. n_samples, n_rows = 800, 4
  28. with cbook.get_sample_data('eeg.dat') as eegfile:
  29. data = np.fromfile(eegfile, dtype=float).reshape((n_samples, n_rows))
  30. t = 10 * np.arange(n_samples) / n_samples
  31. # Plot the EEG
  32. ticklocs = []
  33. ax2 = fig.add_subplot(2, 1, 2)
  34. ax2.set_xlim(0, 10)
  35. ax2.set_xticks(np.arange(10))
  36. dmin = data.min()
  37. dmax = data.max()
  38. dr = (dmax - dmin) * 0.7 # Crowd them a bit.
  39. y0 = dmin
  40. y1 = (n_rows - 1) * dr + dmax
  41. ax2.set_ylim(y0, y1)
  42. segs = []
  43. for i in range(n_rows):
  44. segs.append(np.column_stack((t, data[:, i])))
  45. ticklocs.append(i * dr)
  46. offsets = np.zeros((n_rows, 2), dtype=float)
  47. offsets[:, 1] = ticklocs
  48. lines = LineCollection(segs, offsets=offsets, transOffset=None)
  49. ax2.add_collection(lines)
  50. # Set the yticks to use axes coordinates on the y axis
  51. ax2.set_yticks(ticklocs)
  52. ax2.set_yticklabels(['PG3', 'PG5', 'PG7', 'PG9'])
  53. ax2.set_xlabel('Time (s)')
  54. plt.tight_layout()
  55. plt.show()

下载这个示例