简体   繁体   中英

Add labels ONLY to SELECTED data points in seaborn scatter plot

I have created a seaborn scatter plot and added a trendline to it. I have some datapoints that fall very far away from the trendline (see the ones highlighted in yellow) so I'd like to add data labels only to these points , NOT to all the datapoints in the graph.

Does anyone know what's the best way to do this?

在此处输入图像描述

So far I've found answers to "how to add labels to ALL data points" (see this link ) but this is not my case.

In the accepted answer to the question that you reference you can see that the way they add labels to all data points is by looping over the data points and calling .text(x, y, string) on the axes. You can find the documentation for this method here (seaborn is implemented on top of matplotlib). You'll have to call this method for the selected points.

In your specific case I don't know exactly what formula you want to use to find your outliers but to literally get the ones beyond the limits of the yellow rectangle that you've drawn you could try the following:

for x,y in zip(xarr, yarr):
    if x < 5 and y > 5.5:
        ax.text(x+0.01, y, 'outlier', horizontalalignment='left', size='medium', color='black')

Where xarr is your x-values, yarr your y-values and ax the returned axes from your call to seaborn.

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