舉例:
繪製一個正方體
1 import matplotlib.pyplot as plt
2 from mpl_toolkits.mplot3d import Axes3D
3 import numpy as np
4 import nibabel as nib
5
6 # 建立一個圖形物件
7 fig = plt.figure()
8
9 # 在圖形中新增一個子圖,引數為行數、列數和子圖索引
10 ax = fig.add_subplot(1, 1, 1, projection='3d')
11
12 # 設定座標軸的標籤
13 ax.set_xlabel('X')
14 ax.set_ylabel('Y')
15 ax.set_zlabel('Z')
16
17 data = np.zeros((20, 20, 20))
18
19 data[2:15, 2:15, 2:15] = 1
20
21 # 建立一個新的Nifti1Image物件
22 solid_image = nib.Nifti1Image(data, np.eye(4))
23
24 # 儲存為nii.gz檔案
25 nib.save(solid_image, 'test.nii.gz')
26
27 # 繪製體素圖
28 ax.voxels(data)
29
30 # 顯示圖形
31 plt.show()
顯示結果: