Matplotlib 散点图标签不区分点

Matplotlib scatter plot label not distinguishing points

我正在尝试在散点图中绘制一系列 IP 地址,然后根据它们的 'detections' 为 0 或大于 0.

虽然这个图确实生成了,但它用相同的颜色标记所有点,而不是用恶意标签将它们分开,非常感谢任何帮助!

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()

你没有告诉 scatter() 点应该是什么样子,所以难怪你不会为每个案例得到不同的结果。

您要做的是在一次调用中绘制条件为 "malicious" 的所有点,并指定要使用哪种 marker/color,然后第二次调用条件为非恶意指定一个单独的标记。

像这样:

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()