I have a list as below:
JavaScript
x
2
1
freq = [29342, 28360, 26029, 21418, 20771, 18372, 18239, 18070, 17261, 17102]
2
I want to show the values of n-th and m-th element of the x-axis and draw a vertical line
JavaScript
1
2
1
plt.plot(freq[0:1000])
2
For example in the graph above, the 100th elements on the x-axis – how can I show the values on the line?
I tried to knee but it shows only one elbow. I suggest it is the 50th element? But what is exactly x,y
??
JavaScript
1
7
1
from kneed import KneeLocator
2
kn = KneeLocator(list(range(0, 1000)), freq[0:1000], curve='convex', direction='decreasing')
3
import matplotlib.pyplot as plt
4
kn.plot_knee()
5
#plt.axvline(x=50, color='black', linewidth=2, alpha=.7)
6
plt.annotate(freq[50], xy=(50, freq[50]), size=10)
7
Advertisement
Answer
You might think that everybody knows this library kneed
. Well, I don’t know about others but I have never seen that one before (it does not even have a tag here on SO).
But their documentation is excellent (qhull
take note!). So, you could do something like this:
JavaScript
1
19
19
1
#fake data generation
2
import numpy as np
3
x=np.linspace(1, 10, 100)
4
freq=x**(-1.9)
5
6
#here happens the actual plotting
7
from kneed import KneeLocator
8
import matplotlib.pyplot as plt
9
10
kn = KneeLocator(x, freq, curve='convex', direction='decreasing')
11
xk = kn.knee
12
yk = kn.knee_y
13
14
kn.plot_knee()
15
16
plt.annotate(f'Found knee at x={xk:.2f}, y={yk:.2f}', xy=(xk*1.1, yk*1.1) )
17
18
plt.show()
19
Sample output: