-1

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.

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()
Samolivercz
  • 220
  • 1
  • 4
  • 11
  • 2
    It's pretty hard to provide a full answer without a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) Please provide mockup data, in particular check out [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – Diziet Asahi Feb 14 '18 at 19:26

1 Answers1

1

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.

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()
Diziet Asahi
  • 38,379
  • 7
  • 60
  • 75