【发布时间】:2019-08-05 22:38:24
【问题描述】:
这是我的代码,我需要在橙色曲线上放置标记
halflife = 0.25
resolution_per_second = 1000
values = np.concatenate([np.zeros(resolution_per_second),
np.ones(resolution_per_second * 2),
np.zeros(resolution_per_second),
np.ones(resolution_per_second * 1),
np.zeros(resolution_per_second * 2),
])
t_grid = np.arange(0, len(values)) / resolution_per_second
step = 1.0 / resolution_per_second
k = np.power(0.5, 1 / (halflife * resolution_per_second))
def ema_fixed_step(y, k): #ema method 1 on chart
res = np.zeros_like(y)
curV = y[0]
for i in range(1, len(y) - 1):
curV = k * curV + (1 - k) * y[i]
res[i + 1] = curV
return res
ema1_arr = ema_fixed_step(values, k)
#
w = values != np.roll(values, 1)
w[0] = True
t_new = t_grid[w]
values_new = values[w]
t_extra = [0.6, 1.0001, 1.2, 1.5, 2.9, 4.5, 3.3, 5.5]
t_req = np.sort(np.concatenate([t_new, t_extra]))
def ema_func2(t_req, t, y): #ema method 2 on chart
return np.zeros_like(t_req, dtype=np.double)
ema2_arr = ema_func2(t_req, t_new, values_new)
plt.clf()
plt.step(t_grid, values, '.-', where='post', label='y')
plt.step(t_grid, ema1_arr, '.-', where='post', label='ema method 1')
plt.plot(t_req, ema2_arr, 'o', color='red', markersize=4, label='ema method 2')
plt.grid()
plt.legend()
plt.xlabel('t, seconds')
我有这个
我认为问题出在 EMA2 函数中,但我不明白如何将其编辑为我想要的方式 我验证了 np.where,但没有成功 我也试过用数学来做,但还是不知道 有什么建议吗?
【问题讨论】:
-
是 ema2 您尝试这样做吗?您在寻找什么样的标记?
-
@Reedinationer 好吧,我的猜测是改变 ema2func。我希望这个红色标记在橙色曲线上,而不是在 y = 0 轴上
标签: python matplotlib plot data-visualization visualization