【发布时间】:2019-04-28 10:06:57
【问题描述】:
下面的代码让我绘制 trc-oscilloscope 数据。此外,我正在标记绘图的局部最大值和最小值。
trc 文件:https://ufile.io/0zd2c (200MB)
import matplotlib.pyplot as plt
import pandas as pd
import readTrc
import numpy as np
from scipy.signal import argrelextrema
from scipy import signal
#100mil datapoints
datX, datY, m = readTrc.readTrc('C220180104_ch2_UHF00014.trc')
srx, sry = pd.Series(datX * 1000), pd.Series(datY * 1000)
df = pd.concat([srx, sry], axis = 1)
df.set_index(0, inplace = True)
#Impulse location
x1 = df[1].idxmax() - 0.0005
x2 = df[1].idxmax() + 0.003
df2 = df.loc[x1:x2]
#Locate Maximum
print('Maximum at:', round(df[1].idxmax(), 6), 'ms')
#Local Maxima
n=10 #Every n maximum a Point will be placed
df3_min = df2.iloc[argrelextrema(df2[1].values, np.less_equal, order=n)[0]][1]
df3_max = df2.iloc[argrelextrema(df2[1].values, np.greater_equal, order=n)[0]][1]
plt.scatter(df3_min.index, df3_min, c='r')
plt.scatter(df3_max.index, df3_max, c='g')
#Plot Impulse
df2[1].plot(grid = 1,
linewidth = 1,
figsize = (9,5),
color = 'blue',
legend = False,
xlim = (x1, x2))
plt.xlabel('Time in ms')
plt.ylabel('UHF-Signal in mV')
plt.show()
输出:
现在我想通过最大值绘制一条曲线。我该怎么做?我尝试使用过滤器,但它们“切断”了最高极值(最大值)。
编辑:
添加这些代码连接最大值:
df3_max.plot()
输出:
现在如果我尝试应用黄油过滤器:
b, a = signal.butter(5, 0.1)
y2 = signal.filtfilt(b,a, df3_max[1].values)
df3_max = pd.DataFrame(y2, index=df3_max.index)
我得到一个错误:
Traceback (most recent call last):
File "/home/artur/.local/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 3078, in get_loc
return self._engine.get_loc(key)
File "pandas/_libs/index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 379, in pandas._libs.hashtable.Float64HashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 385, in pandas._libs.hashtable.Float64HashTable.get_item
KeyError: 1.0
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/artur/Desktop/shard_plot/UHF_impulse_plot.py", line 38, in <module>
y2 = signal.filtfilt(b,a, df3_max[1].values)
File "/home/artur/.local/lib/python3.6/site-packages/pandas/core/series.py", line 767, in __getitem__
result = self.index.get_value(self, key)
File "/home/artur/.local/lib/python3.6/site-packages/pandas/core/indexes/numeric.py", line 358, in get_value
loc = self.get_loc(k)
File "/home/artur/.local/lib/python3.6/site-packages/pandas/core/indexes/numeric.py", line 419, in get_loc
tolerance=tolerance)
File "/home/artur/.local/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 3080, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas/_libs/index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 379, in pandas._libs.hashtable.Float64HashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 385, in pandas._libs.hashtable.Float64HashTable.get_item
KeyError: 1.0
【问题讨论】:
-
plot函数不能满足您的需要吗?plt.plot(df3_max.index, df3_max)甚至df3_max.plot()?不过我可能不明白这个问题。 -
除了
plt.scatter(df3_max.index, df3_max, c='g'),您还可以绘制plt.plot(df3_max.index, df3_max, c='g')。这是否已经解决了它,还是您的意思是通过您的最大值进行一些样条拟合? -
df3_max 是一个数据框,除了最大值之外,没有填充。我不得不这样做是因为我要绘制的最大值(圆圈)必须与基本 df 具有相同的索引。
-
我不知道你的数据框的格式,但似乎你只需要
df3_max而不是df3_max[1]。 -
@busybear:你可能想添加这个作为答案:)
标签: python pandas numpy matplotlib scipy