What should I add to add red-markers on the orange curve in Matplotlib?

帅比萌擦擦* 提交于 2019-12-24 07:28:25

问题


Here is my code and I need put markers on the orange curve

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

And I've got this

I think the problem is in EMA2 function but I can't understand how to edit it to be the way I want I trued np.where, but it didn't work out I also tried to made it using mathematics, but still have no clue Any suggestions ?


回答1:


  1. You didn't include imports, but luckily I figured them out
  2. I had to remove a value from t_extra as there was no corresponding point

That said, here is what I came up with

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

Output: Basically I just made a list, and if the times matched I grabbed the y-value at the appropriate index (cause the x and y must have same index for plotting). Then I plotted these and ensured they were the top level of the graph via zorder=10



来源:https://stackoverflow.com/questions/55172607/what-should-i-add-to-add-red-markers-on-the-orange-curve-in-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!