2

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?

piRSquared
  • 285,575
  • 57
  • 475
  • 624
Alex Kinman
  • 2,437
  • 8
  • 32
  • 51
  • Possible duplicate http://stackoverflow.com/questions/21654635/scatter-plots-in-pandas-pyplot-how-to-plot-by-category – johnchase Oct 25 '16 at 00:10

2 Answers2

4

One approach is to plot twice on the same axes. First we plot only the "good" points, then we plot only the "bad". The trick is to use the ax keyword to the scatter method, as such:

ax = df[df.result == 'Good'].plot.scatter('x', 'y', color='green')
df[df.result == 'Bad'].plot.scatter('x', 'y', ax=ax, color='red')

scatter plot

jme
  • 19,895
  • 6
  • 41
  • 39
4
df.plot.scatter('x', 'y', c=df.result.map(dict(Good='green', Bad='red')))

enter image description here

piRSquared
  • 285,575
  • 57
  • 475
  • 624