'How to find (and select) a specific point of a graph in Python
The code below computes the autocorrelation using plt.acorr for a time-series.
My aim is to find the first value (vertical bar) on the x-axis where the graph of the autocorrelation goes below 1/e (= 0.3679).
In the example shown below (see image), the x-axis value the code had to select would be the very first vertical bar located on the x-axis at x=1 (or at x=-1), since the graph of the autocorrelation passes below 1/e here.
In other words, the code would have to select the value 1 (the negative counterpart -1 is not needed).
What would be an appropriate way to code this?
import numpy as np
import matplotlib.pyplot as plt
Timeseries = [-0.213399, 0.383858, 0.162265, 0.393239, 0.018605,
-0.241237, 0.0230115, 0.226046, 0.137763, -0.0818484, 0.0341396,
-0.460055, -0.201813, 0.0728111, -0.259836, -0.0302774, 0.125858]
Threshold = 0.367879441171
plt.axhline(y=Threshold, color='r', linestyle='-')
# Autocorrelation (AC)
plt.acorr(x=Timeseries, maxlags=4, normed=True, color="blue", lw=1.5)
plt.title("Autocorrelation")
plt.show()
Solution 1:[1]
plt.acorr can return the resulting AC like so:
lags, c, line, b = plt.acorr(x=Timeseries, maxlags=4, normed=True, color="blue", lw=1.5)
Hence, you can do:
indices_below = np.where(c < 1/np.exp(1))[0]
index_zero = np.where(lags == 0)[0]
first_point = lags[np.argmin(np.abs(indices_below - index_zero))] # = -1
The above returns -1 because it is the "first" from left to right between -1 and +1, but you could just flip the arrays or add another condition if you really want to get 1.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

