【问题标题】:Python: plotting across time with a single column filePython:使用单列文件跨时间绘图
【发布时间】:2022-01-02 10:33:08
【问题描述】:

我必须编写一个函数,让我从一系列值中找到局部最大值和最小值。

函数的数据是每个“峰值”的 x、y。 输出是 4 个包含 x、y 最大和最小“峰值”的向量。

要找到最大峰值,我必须“站在”每个数据点上并检查它是否小于两侧的邻居,以确定它是否是峰值(保存为最大/最小峰值)。 两端点只有1个邻居,本次分析不考虑。

然后编写一个程序来读取数据文件并调用函数来计算峰值。程序必须生成一个图表,显示输入的数据和计算出的峰值。

第一个文件是一个 (2001,) 大小的 float64 数组。所有数据都在第 0 列。该文件表示时间信号的幅度,采样频率为 200Hz。假设初始时间为 0。

Graph should look like this

程序还必须生成一个显示 2 个表格的 .xls 文件; 1 个具有最小峰值,另一个具有最大峰值。每个表格必须有标题,由 2 列组成,一列是峰值出现的时间,另一列是每个峰值的幅度。

不允许熊猫。

first file is a .txt file, and is single column, 2001 rows total
0
0.0188425
0.0376428
0.0563589
0.0749497
0.0933749
0.111596
0.129575
0.147277
0.164669
0.18172
...

当前尝试:

import numpy as np
import matplotlib.pyplot as plt

filename = 'location/file_name.txt'
T = np.loadtxt(filename,comments='#',delimiter='\n')

x = T[::1]  # all the files of column 0 are x vales

a = np.empty(x, dtype=array)
y = np.linspace[::1/200]

X, Y = np.meshgrid(x,y)

【问题讨论】:

标签: python matplotlib graph vectorization spyder


【解决方案1】:

这可以满足您的要求。我不得不生成随机数据,因为你没有分享你的。您当然可以根据最小值和最大值构建电子表格。

import numpy as np
import matplotlib.pyplot as plt

#filename = 'location/file_name.txt'
#T = np.loadtxt(filename,comments='#',delimiter='\n')
#
#y = T[::1]  # all the files of column 0 are x vales

y = np.random.random(200) * 2.0

minima = []
maxima = []
for i in range(0,y.shape[0]-1):
    if y[i-1] < y[i] and y[i+1] < y[i]:
        maxima.append( (i/200, y[i]) )
    if y[i-1] > y[i] and y[i+1] > y[i]:
        minima.append( (i/200, y[i]) )

minima = np.array(minima)
maxima = np.array(maxima)
print(minima)
print(maxima)

x = np.linspace(0, 1, 200 )

plt.plot( x, y )
plt.scatter( maxima[:,0], maxima[:,1] )
plt.show()

【讨论】:

    猜你喜欢
    • 2016-02-14
    • 1970-01-01
    • 2018-03-26
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 2019-08-21
    • 2018-10-22
    • 1970-01-01
    相关资源
    最近更新 更多