简体   繁体   中英

How to annotate(xy coordinates) a percentile plot of nd.array?

Im trying to plot percentiles of nd.array, and i want to consider all the points above elbow point. so if my elbow point is at 83%, i want to get the y coordinate at point of 83rd percentile(hence the question) so set it as a threshold and get all points above that.

i tried .annotate, but am stuck. please help.

from matplotlib import mlab
p = np.array([0,10,20,30,40,50,60,70,80,83,84,85,90])
pchange=np.array([1,2,3,6,5,8,9,7,4,5,6,9,8,5,2,3,6,4,25,36,14,65,98,98,54,25,26,23,24,27,28,26,24,262,1,156,31,51,351,651,35,153,135,1,5,31,68,3,5,61,354,685,16,813,51,685,681,35,68,135,1685,1354,135,415,135,153,413,513,56,513,213,651,354,51,35,135,135,135,438,535,468,53,8,35,4,648,468,535,468,46,8,498,498,749,8798,798,79,8798,7,979,879,8,97,9,79,7,9798,798,78,979,87,974,65,498,46,8,98,79,878,978,65,984,98,49,9,569,949,888,888,888,888,888,888,888,888,888,888,888,888,888,888,888,888,888,888,888,888,888,888,888,888,888,888,888,888,888,888,888,888,888,888,888,888]) 
perc = mlab.prctile(pchange, p)
print(perc)
plt.plot(perc)
plt.plot((len(perc)-1) * p/100., perc, 'ro')
plt.xticks((len(perc)-1) * p/100., map(str, p))
for a,b in enumerate(pchange):
    plt.annotate(b,(perc[a],p[a]),xytext=(perc[a],p[a]))
plt.show()

Based on the current information I am not entirely sure of what you are after. Your x-axis is not intuitive. When I test your code I only get an error with your annotation because you should loop over the percentages and not the total data. See the code below and the annotate example for more help. If this is not what you were looking for then please clarify your question and I will update my answer.

import numpy as np
from matplotlib import pyplot as plt

p = np.array([0,10,20,30,40,50,60,70,80,83,84,85,90])

pchange=np.array([1,2,3,6,5,8,9,7,4,5,6,9,8,5,2,3,6,4,25,36,14,65,98,98,54,25,26,23,24,27,28,26,24,262,1,\
              156,31,51,351,651,35,153,135,1,5,31,68,3,5,61,354,685,16,813,51,685,681,35,68,135,1685,1354,\
              135,415,135,153,413,513,56,513,213,651,354,51,35,135,135,135,438,535,468,53,8,35,4,648,468,535,\
              468,46,8,498,498,749,8798,798,79,8798,7,979,879,8,97,9,79,7,9798,798,78,979,87,974,65,498,46,8,\
              98,79,878,978,65,984,98,49,9,569,949,888,888,888,888,888,888,888,888,888,888,888,888,888,888,888,\
              888,888,888,888,888,888,888,888,888,888,888,888,888,888,888,888,888,888,888,888,888,])

perc = np.percentile(pchange,p)


plt.plot(p, perc)
plt.plot(p, perc, 'ro')

for a,b in enumerate(perc):
    plt.annotate('%2.2f'%b,(p[a],perc[a]),xytext=(p[a],perc[a])) 
    #Note you may want to add an offset to your x,y since labeling every point
    # makes the plot cluttered. I added %2.2f to truncate the values some.

plt.show()

This results in the following figure

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM