Skip to content
Advertisement

how to label the minimum value on a regular line plot

Here is a line plot generated from the following code:

test_error_rates = [0.10777777777777775,
 0.09999999999999998,
 0.07444444444444442,
 0.07666666666666666,
 0.07222222222222219,
 0.06444444444444442,
 0.06444444444444442,
 0.06222222222222218,
 0.06000000000000005,
 0.06222222222222218,
 0.06222222222222218,
 0.06000000000000005,
 0.06222222222222218,
 0.06222222222222218,
 0.06000000000000005,
 0.05666666666666664,
 0.05555555555555558,
 0.05555555555555558,
 0.053333333333333344,
 0.053333333333333344,
 0.054444444444444406,
 0.05111111111111111,
 0.054444444444444406,
 0.054444444444444406,
 0.05666666666666664,
 0.05666666666666664,
 0.05555555555555558,
 0.05777777777777782,
 0.05777777777777782]

plt.plot(range(1,30),test_error_rates)
plt.ylabel('ERROR RATE')
plt.xlabel('K Neighbor')

enter image description here

Now, I want to label (maybe add a small circle in red around that point) the minimum point that falls in between K = 20 and K = 25. How should I do this?

Advertisement

Answer

here you go:

test_error_rates = [0.10777777777777775,
 0.09999999999999998,
 0.07444444444444442,
 0.07666666666666666,
 0.07222222222222219,
 0.06444444444444442,
 0.06444444444444442,
 0.06222222222222218,
 0.06000000000000005,
 0.06222222222222218,
 0.06222222222222218,
 0.06000000000000005,
 0.06222222222222218,
 0.06222222222222218,
 0.06000000000000005,
 0.05666666666666664,
 0.05555555555555558,
 0.05555555555555558,
 0.053333333333333344,
 0.053333333333333344,
 0.054444444444444406,
 0.05111111111111111,
 0.054444444444444406,
 0.054444444444444406,
 0.05666666666666664,
 0.05666666666666664,
 0.05555555555555558,
 0.05777777777777782,
 0.05777777777777782]

min_x = np.argmin(test_error_rates) + 1
min_y = np.min(test_error_rates)

plt.plot(range(1,30),test_error_rates)
plt.scatter(min_x, min_y,c='r', label='minimum')
plt.legend()
plt.ylabel('ERROR RATE')
plt.xlabel('K Neighbor')

enter image description here

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement