初めに、解析結果をMatlab形式で保存します。
Pythonで開くことができる*.mat形式のファイルが作成されます。
例1:Onscaleデータからapmx配列を抽出し、Pythonで曲線を表示する
例として、以下のmatlab形式のテスト結果ファイルを使います。
Download: pzt-simple-tuto-2d.mat
apmx配列に含まれるデータをPythonで結果処理します(最大音圧)。OnScale Post-processで表示すると以下のようなapmx配列です。
次のPythonスクリプトで開いて結果処理します。
#!/usr/bin/python import numpy as np import matplotlib as ml import matplotlib.pyplot as plt from scipy.io import loadmat x = loadmat('pzt-simple-tuto-2d.mat') #Check the keys of the x array print(x.keys()) #Extract the x and y coordinates xcrd = x['d1_0006160_xcrd'] ycrd = x['d1_0006160_ycrd'] #Find the i value corresponding to a certain xval and save it to ival xval = 150e-05 sampling = round(float(xcrd[2]-xcrd[1]),8) for i in range(len(xcrd)): if(xval > xcrd[i]-sampling and xval < xcrd[i]+sampling): ival = i print(ival) #Plot the aprs in function of ival plt.plot(x['d1_0006160_apmx'][ival]) plt.savefig('d1_0006160_apmx.png')
このPythonスクリプトで結果処理し、位置x = 150e-05で曲線を抽出します。これは結果の曲線です。
注:このチュートリアルに従うには、numpy、matplotlib、およびscipyモジュールだけでなくPythonもインストールする必要があります。
Pythonスクリプトをダウンロードする
注: matplotlibを使用してプロットする方法の詳細が必要な場合は、このページをご確認ください。
例2:3Dインタラクティブプロットの表示
以下のようなインタラクティブな3次元プロットを表示させることもできます。
Pythonコードを以下に示します。
#!/usr/bin/env python """ Demonstration of 3D post processing. """ import numpy as np import matplotlib.pyplot as plt from mpl_toolkits import mplot3d from scipy.io import loadmat plt.style.use('seaborn-darkgrid') # Load Data data = loadmat('pzt-simple-tuto-2d.mat') # Extract (X, Y) coordinates. Cut off the final coord so shapes match. X = data['d1_0006160_xcrd'].flatten()[:-1] Y = data['d1_0006160_ycrd'].flatten()[:-1] # The target X value we are looking for x_val = 150e-05 # Find the index of this X value dx = X[1] - X[0] x_index = np.argmax((x_val > X - dx) & (x_val < X + dx)) # Create 3D coordinate grid xx, yy = np.meshgrid(X, Y) # Initialize figure fig = plt.figure(figsize=(12, 8)) ax = plt.axes(projection='3d') # Plot the apmx surface with some nice colors zz = data['d1_0006160_apmx'].T ax.plot_surface(xx, yy, zz, alpha=.9, rstride=1, cstride=1, cmap='viridis') # Plot the 2D curve of our X value's cross-section slice x = np.repeat(X[x_index], len(Y)) y = Y z = zz[:, x_index] ax.plot(x, y, z, color='k', ls='--', zorder=10) # Label and display ax.set_title("X[{:d}] = {:e}".format(x_index, X[x_index])) ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') plt.show()