制作/保存图表,如果图表符合条件,则该图表将从X点开始

时间:2018-07-22 15:12:57

标签: python matplotlib

因此,我是python的新手(并发布在StackOverflow上),我创建了一个程序,该程序接收2列数据表,并根据其是否满足条件将y值转换为1或0。然后,我将这些值搜索为最长的连续值。我想绘制一个图表,以显示最长连续区域的原始值的图形,以及最长连续区域的20之前和之后的数据点,有什么建议吗?

抱歉,如果这可能含糊不清,我会尽力回答您可能遇到的问题,但是,我确实将我的代码添加到了这篇文章中。

import numpy as np
from itertools import groupby
import matplotlib.pyplot as plt

voltageslothigh = 0.9
voltageslotlow = 0.1

DefaultData = np.genfromtxt(r'''D:\DrRoberson\1kHz\1kHz_01.csv''', delimiter=',', skip_header=2) #Loads in the CSV file as an array

MinimumValues = np.amin(DefaultData, axis=0)#Find Minimum Values so that they can be added to all of the voltages to have a lowest point of zero
MinimumVoltage = MinimumValues[1]
DefaultVoltages = DefaultData[:,1]
DefaultTimes = DefaultData[:,0]
NewVoltages = np.array(DefaultVoltages) - MinimumVoltage 
MaximumVoltage = np.amax(NewVoltages, axis=0) #Determines the maximum voltage and time value [time irrelevant]

ScaledVoltages = np.array(NewVoltages) / MaximumVoltage #Calculating on a spectrum of 0-1
MaximumScaledVoltage = np.amax(ScaledVoltages)
MinimumScaledVoltage = np.amin(ScaledVoltages)

ScaledVoltages[ScaledVoltages < voltageslotlow] = 0
ScaledVoltages[ScaledVoltages > voltageslothigh] = 0
ScaledVoltages[ScaledVoltages > 0] = 1
ScaledData = np.column_stack((DefaultTimes, ScaledVoltages))

def len_iter(items):
    return sum(1 for _ in items)

def consecutive_one(data):
    return max(len_iter(run) for val, run in groupby(data) if val)

TimeDifference = DefaultTimes[1] - DefaultTimes[0]

RiseTime = TimeDifference * consecutive_one(ScaledVoltages)

print('ConsecutiveOnes:', consecutive_one(ScaledVoltages))

print('RiseTime:', RiseTime)

fig = plt.figure(figsize=(200,200))
plt.plot(DefaultTimes, ScaledVoltages)
ax = plt.subplot(111)
ax.set_ylim(1e-300,1e1)
ax.set_xlim(-5,5)
plt.yscale('log')
plt.xlabel('Time, Miliseconds, Rise Time = ')
plt.ylabel('ScaledVoltage')
plt.grid()
fig.savefig('VoltageTime.pdf', dpi = 500)

0 个答案:

没有答案