1

I have a list as below:

freq = [29342, 28360, 26029, 21418, 20771, 18372, 18239, 18070, 17261, 17102]

I want to show the values of n-th and m-th element of the x-axis and draw a vertical line

plt.plot(freq[0:1000])

enter image description here

For example in the graph above, the 100th elements on the x-axis - how can I show the values on the line?

enter image description here

I tried to knee but it shows only one elbow. I suggest it is the 50th element? But what is exactly x,y??

from kneed import KneeLocator
kn = KneeLocator(list(range(0, 1000)), freq[0:1000], curve='convex', direction='decreasing')
import matplotlib.pyplot as plt
kn.plot_knee()
#plt.axvline(x=50, color='black', linewidth=2, alpha=.7) 
plt.annotate(freq[50], xy=(50, freq[50]), size=10)

enter image description here

6
  • 1
    That '100' is fixed? If you want to show a vertical line you can use plt.axvline(x=100, color='black', linewidth=2, alpha=.7) plt.annotate(freq[100], xy=(100, freq[100]), size=8) Commented Nov 24, 2020 at 15:32
  • 1
    @solopiu Or define your own annotation function that takes ax, a list of x-indexes, and freq as parameters and annotates your curve as you just have shown. Surprisingly, I did not find easily a duplicate, so go ahead - I will upvote it. Commented Nov 24, 2020 at 16:01
  • Thanks a lot, it works, Actually I want to apply zipf distribution of my corpus and find elbow points values. I draw first elbow point with kneelocator but ı did not found the value of elbow. Commented Nov 24, 2020 at 16:23
  • Sliiiightly different question. Commented Nov 24, 2020 at 16:24
  • kn.plot_knee() shows an elbow, but what is the value of it? I think if I merge two plot, I can guess the values x and y on the elbow exactly. Commented Nov 24, 2020 at 16:33

1 Answer 1

3

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:

#fake data generation
import numpy as np
x=np.linspace(1, 10, 100)
freq=x**(-1.9) 

#here happens the actual plotting
from kneed import KneeLocator
import matplotlib.pyplot as plt

kn = KneeLocator(x, freq, curve='convex', direction='decreasing')
xk = kn.knee
yk = kn.knee_y

kn.plot_knee()

plt.annotate(f'Found knee at x={xk:.2f}, y={yk:.2f}', xy=(xk*1.1, yk*1.1) )

plt.show()

Sample output:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.