How to alter function so it won't be on the one line but be curved using matplotlib? [on hold]












-1












$begingroup$


I have that code:



from matplotlib import pyplot as plt
import numpy as np

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


That's the part of code and that's the result:



enter image description here



I need to complete ema_func2 so the dots would be on the orange line. How is it possible to complete function for those red dots appear on the orange curve. So, I need to take some piece from ema_fixed_step and somehow alter it according to t_req. How could it be done?










share|improve this question







New contributor




SomeOneInteresting is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$



put on hold as off-topic by Vogel612 4 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Vogel612

If this question can be reworded to fit the rules in the help center, please edit the question.












  • 1




    $begingroup$
    Welcome to Code Review! I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
    $endgroup$
    – Vogel612
    4 hours ago
















-1












$begingroup$


I have that code:



from matplotlib import pyplot as plt
import numpy as np

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


That's the part of code and that's the result:



enter image description here



I need to complete ema_func2 so the dots would be on the orange line. How is it possible to complete function for those red dots appear on the orange curve. So, I need to take some piece from ema_fixed_step and somehow alter it according to t_req. How could it be done?










share|improve this question







New contributor




SomeOneInteresting is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$



put on hold as off-topic by Vogel612 4 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Vogel612

If this question can be reworded to fit the rules in the help center, please edit the question.












  • 1




    $begingroup$
    Welcome to Code Review! I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
    $endgroup$
    – Vogel612
    4 hours ago














-1












-1








-1





$begingroup$


I have that code:



from matplotlib import pyplot as plt
import numpy as np

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


That's the part of code and that's the result:



enter image description here



I need to complete ema_func2 so the dots would be on the orange line. How is it possible to complete function for those red dots appear on the orange curve. So, I need to take some piece from ema_fixed_step and somehow alter it according to t_req. How could it be done?










share|improve this question







New contributor




SomeOneInteresting is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$




I have that code:



from matplotlib import pyplot as plt
import numpy as np

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


That's the part of code and that's the result:



enter image description here



I need to complete ema_func2 so the dots would be on the orange line. How is it possible to complete function for those red dots appear on the orange curve. So, I need to take some piece from ema_fixed_step and somehow alter it according to t_req. How could it be done?







python data-visualization matplotlib






share|improve this question







New contributor




SomeOneInteresting is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




SomeOneInteresting is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




SomeOneInteresting is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 4 hours ago









SomeOneInterestingSomeOneInteresting

1




1




New contributor




SomeOneInteresting is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





SomeOneInteresting is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






SomeOneInteresting is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




put on hold as off-topic by Vogel612 4 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Vogel612

If this question can be reworded to fit the rules in the help center, please edit the question.







put on hold as off-topic by Vogel612 4 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Vogel612

If this question can be reworded to fit the rules in the help center, please edit the question.








  • 1




    $begingroup$
    Welcome to Code Review! I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
    $endgroup$
    – Vogel612
    4 hours ago














  • 1




    $begingroup$
    Welcome to Code Review! I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
    $endgroup$
    – Vogel612
    4 hours ago








1




1




$begingroup$
Welcome to Code Review! I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
$endgroup$
– Vogel612
4 hours ago




$begingroup$
Welcome to Code Review! I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
$endgroup$
– Vogel612
4 hours ago










0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

is 'sed' thread safe

How to make a Squid Proxy server?