Alex Kinman Alex Kinman - 28 days ago 15
Python Question

Color coding or labelling the scatter plot of a pandas dataframe?

I have a data frame that I am plotting in pandas:

import pandas as pd
df = pd.read_csv('Test.csv')
df.plot.scatter(x='x',y='y')


the data frame has 3 columns

x y result
0 2 5 Good
1 3 2 Bad
2 4 1 Bad
3 1 1 Good
4 2 23 Bad
5 1 34 Good


I want to format the scatter plot such that each point is green if df['result']='Good' and red if df['result']='Bad'.

Can do that using pd.plot or is there a way of doing it using pyplot?

Answer
df.plot.scatter('x', 'y', c=df.result.map(dict(Good='green', Bad='red')))

enter image description here