Reputation: 615
I have a df
such as this:
Date Sig
2020-06-16 09:00:00 1
2020-06-16 13:00:00 0
2020-06-16 17:00:00 -1
2020-06-16 21:00:00 -1
2020-06-17 01:00:00 0
2020-06-17 05:00:00 1
2020-06-17 09:00:00 1
2020-06-17 13:00:00 0
2020-06-17 17:00:00 0
2020-06-17 21:00:00 -1
What I'm trying to do is a simple scatter plot in Matplotlib with the index as the X axis and the Sig
column on the Y, but only showing 1s and -1s, with 1 colored blue and -1 colored red. But I don't want a 0 row to be omitted from the scatter plot if that makes sense.
I can obviously just do the scatter plot thusly 'plt.scatter(x=df.index, y=df['Sig'])' but that of course plots all numbers in Sig
and doesn't color code them.
Any help would be really appreciated. Cheers
Upvotes: 0
Views: 425
Reputation: 4629
plt.scatter
supports iterables of colors, so you can actually give each point an independent color.
Here, we will simply have a dictionary with three choices dictated by the sign of your data:
colors = {-1: "red", 0: "black", +1: "blue"}
plt.scatter(x=df.index, y=df["Sig"], color=[colors[sign] for sign in df["Sig"]])
Upvotes: 1
Reputation: 150745
You can do a groupby on filtered data like this:
# datetime data type is recommended
df['Date'] = pd.to_datetime(df['Date'])
colors = ['r','b']
fig, ax = plt.subplots()
for c, (k,d) in zip(colors, df[df['Sig']!=0].groupby('Sig')):
d.plot.scatter(x='Date',y='Sig',label=k, ax=ax, c=c)
Output:
Upvotes: 1