我的問題是計算 FT-IR 分析中峰下的面積。我通常使用 Origin,但我想看看使用 Python 是否能獲得更好的結果。我正在使用的資料在此處鏈接,代碼如下。我面臨的問題是,我不知道如何找到峰的起點和終點來計算面積以及如何設定基線。
我發現了這個關于如何計算多個峰下面積的回答問題,但我不知道如何在我的代碼中實作它:
uj5u.com熱心網友回復:
好的,我已經快速將另一篇文章中的代碼添加到您的開頭并檢查它是否有效。不幸的是,您鏈接的檔案不適用于您的代碼,因此我不得不在開始時更改一些內容以使其作業(以一種非常不優雅的方式,因為我真的不知道如何使用資料幀)。如果你的本地檔案不同,用這種方式處理檔案不起作用,那么就用你的來交換我的開頭。
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import peakutils
df = pd.read_csv(r'CuCO3.csv', skiprows=5)
data = np.asarray([[float(y) for y in x[0].split(",")] for x in df.to_numpy()])
Wavenumber = np.arange(700, 916)
Absorbance = data[700:916,1]
indices = peakutils.indexes(Absorbance, thres=0.35, min_dist=0.1)
peak_values = [Absorbance[i] for i in indices]
peak_Wavenumbers = [Wavenumber[i] for i in indices]
plt.figure()
plt.scatter(peak_Wavenumbers, peak_values)
plt.plot(Wavenumber, Absorbance)
plt.show()
ixpeak = Wavenumber.searchsorted(peak_Wavenumbers)
ixmin = np.array([np.argmin(i) for i in np.split(Absorbance, ixpeak)])
ixmin[1:] = ixpeak
mins = Wavenumber[ixmin]
# split up the x and y values based on those minima
xsplit = np.split(Wavenumber, ixmin[1:-1])
ysplit = np.split(Absorbance, ixmin[1:-1])
# find the areas under each peak
areas = [np.trapz(ys, xs) for xs, ys in zip(xsplit, ysplit)]
# plotting stuff
plt.figure(figsize=(5, 7))
plt.subplots_adjust(hspace=.33)
plt.subplot(211)
plt.plot(Wavenumber, Absorbance, label='trace 0')
plt.plot(peak_Wavenumbers, Absorbance[ixpeak], ' ', c='red', ms=10, label='peaks')
plt.plot(mins, Absorbance[ixmin], 'x', c='green', ms=10, label='mins')
plt.xlabel('dep')
plt.ylabel('indep')
plt.title('Example data')
plt.ylim(-.1, 1.6)
plt.legend()
plt.subplot(212)
plt.bar(np.arange(len(areas)), areas)
plt.xlabel('Peak number')
plt.ylabel('Area under peak')
plt.title('Area under the peaks of trace 0')
plt.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/324253.html
標籤:Python 熊猫 麻木的 matplotlib
