对于用下面的代码生成的绘图,我想通过pandas逻辑生成一个信号。在
当曲线比上一个局部最小值高出+3个点(或更多)时,输出信号应从-4变为-2。当曲线比上一个局部最大值低2个点(或更少)时,它应该从-2变回-4。在
图1显示了由下面代码生成的曲线。图2大致显示了输出信号的外观。在
代码:
import matplotlib
matplotlib.use('QT5Agg')
import matplotlib.pyplot as plt
import numpy as np
a = np.arange(5)
b = np.arange(5, -4, -1)
c = np.arange(-4, 7, .5)
d = np.arange(7, 2, -1)
e = np.arange(2, 6, .2)
f = np.arange(6, -3, -1)
g = np.arange(-3, 2, .25)
r1 = np.append(a, b)
r2 = np.append(r1, c)
r3 = np.append(r2, d)
r4 = np.append(r3, e)
r5 = np.append(r4, f)
r6 = np.append(r5, g)
plt.rcParams['font.size'] = 6
fig, ax1 = plt.subplots()
ax1.plot(r6,'g-o',markersize=3)
plt.annotate('start upward', xy=(0,0), textcoords='data',)
plt.annotate('end upward', xy=(3,3), textcoords='data',)
plt.annotate('start downward', xy=(5,5), textcoords='data',)
plt.annotate('end downward', xy=(7,3), textcoords='data',)
plt.annotate('start upward', xy=(14,-4), textcoords='data',)
plt.annotate('end upward', xy=(20,-1), textcoords='data',)
plt.annotate('start downward', xy=(36,7), textcoords='data',)
plt.annotate('end downward', xy=(38,5), textcoords='data',)
plt.annotate('start upward', xy=(41,2), textcoords='data',)
plt.annotate('end upward', xy=(56,5), textcoords='data',)
plt.annotate('start downward', xy=(61,6), textcoords='data',)
plt.annotate('end downward', xy=(63,4), textcoords='data',)
plt.annotate('start upward', xy=(70,-3), textcoords='data',)
plt.annotate('end upward', xy=(82,0), textcoords='data',)
ax1.minorticks_on()
ax1.grid(b=True, which='major', color='g', linestyle='-')
ax1.grid(b=True, which='minor', color='y', linestyle='--')
plt.show()
IIUC:
我想你想要这个:
输出:
相关问题 更多 >
编程相关推荐