简体   繁体   English

Matplotlib散点图标签不区分点

[英]Matplotlib scatter plot label not distinguishing points

I'm trying to plot a series of IP addresses within a scatter plot and then label them based on their 'detections' which are either 0 or greater than 0.我试图在散点图中绘制一系列 IP 地址,然后根据它们为 0 或大于 0 的“检测”标记它们。

Although this plot does get generated, it colours all points the same, rather than separating them by the label of malicious, any help is much appreciated!尽管此图确实生成了,但它为所有点着色相同,而不是通过恶意标签将它们分开,非常感谢任何帮助!

df = pd.io.sql.read_sql('SELECT Date_scanned, IP, Detections FROM URLs', con=conn)
df['Date_scanned']=df['Date_scanned'].str.split(" ").str[0]
detections = df['Detections'].values

for row in df:
    for x in detections:
        if x > 0:
            malicious = "Yes"
        else:
            malicious = "No"

    plt.scatter(df['Date_scanned'], df['IP'], label=malicious)
plt.legend(loc='best')
plt.show()

You're not telling scatter() how the points should look like, so it's no wonder you don't get a different result for each of your cases.您没有告诉scatter()点应该是什么样子,所以难怪您不会为每个案例得到不同的结果。

What you want to do instead is plot ALL the points with condition "malicious" in one call and specify which kind of marker/color to use, then a second call for the condition non-malicious specifying a separate marker.相反,您想要做的是在一次调用中绘制条件为“恶意”的所有点,并指定要使用的标记/颜色类型,然后对非恶意条件的第二次调用指定一个单独的标记。

Something like this:像这样的东西:

df = pd.io.sql.read_sql('SELECT Date_scanned, IP, Detections FROM URLs', con=conn)
df['Date_scanned']=df['Date_scanned'].str.split(" ").str[0]

plt.scatter(df.loc[df.Detections>0,'Date_scanned'], df.loc[df.Detections>0,'IP'], marker='o', color='red', label="YES")
plt.scatter(df.loc[df.Detections≤0,'Date_scanned'], df.loc[df.Detections≤0,'IP'], marker='x', color='blue', label="NO")

plt.legend(loc='best')
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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