Python

Transpose multiple array into single array

쿠피1905 2023. 11. 14. 17:19

Python 을 계산 결과를 다른 프로그램으로 그래프로 그리기 위해 결과를 data파일로 저장하는 것이 필요하다.

만약, 결과가 여러개의 배열로 되어있다면, 하나의 배열로 합쳐서 결과를 출력하면, 그래프를 그릴때도 용이하다.

import numpy as np
from scipy import signal
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 100)
y = np.sin(x)

# Savitzky-Golay 필터를 사용한 스무딩
smoothed_y = signal.savgol_filter(y, window_length=5, polyorder=3)

#plot x,y in red-colored dotted line and x, smoothed_y in blue colored circle marker
plt.plot(x,y, 'r--', x, smoothed_y, 'bo')
plt.show()

# to transpose the output of x, y, and smoothed_y in one array
combined=np.transpose((x,y,smoothed_y))

#to save combined array as swave.text. Option is seven digit after decimal point with exponential form. separation is black. 
np.savetxt('swave.txt',combined,fmt='%0.7e', delimiter=' ')

 

transpose나 savetxt는 numpy 모듈이 필요하다.

 

 

'Python' 카테고리의 다른 글

Documentation of Python  (0) 2023.11.16
Python을 이용한 Hecht equation fitting  (0) 2022.12.26