gpt4 book ai didi

python - 我应该添加什么来在 Matplotlib 的橙色曲线上添加红色标记?

转载 作者:行者123 更新时间:2023-12-01 01:07:48 25 4
gpt4 key购买 nike

这是我的代码,我需要在橙色曲线上放置标记

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')

我有这个

enter image description here

我认为问题出在 EMA2 函数中,但我不明白如何将其编辑为我想要的方式我对 np.where 进行了验证,但没有成功我也尝试用数学来实现,但仍然不知道有什么建议吗?

最佳答案

  1. 您没有包含导入内容,但幸运的是我找到了它们
  2. 我必须从 t_extra 中删除一个值,因为没有对应的点

也就是说,这就是我的想法

import numpy as np
import matplotlib.pyplot as plt

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.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')

markers_y = []
for t in t_grid:
if t in t_req:
index = list(t_grid).index(t)
markers_y.append(ema1_arr[index])

plt.scatter(t_req, markers_y, color='red', label='markers', zorder=10)

plt.grid()
plt.legend()
plt.xlabel('t, seconds')

plt.show()

输出: enter image description here基本上我只是做了一个列表,如果时间匹配,我会在适当的索引处获取 y 值(因为 x 和 y 必须具有相同的索引才能绘图)。然后我绘制了这些并通过 zorder=10

确保它们位于图表的顶层

关于python - 我应该添加什么来在 Matplotlib 的橙色曲线上添加红色标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55172607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com