Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot data from pandas DataFrame, colour of points dependant on a column

I have a pandas DataFrame with 3 columns, shown below.

col1 value flag 1 0 0 2 0.03915 0 3 0.13 1

I want to create a scatterplot from this dataframe where col1 is the x axis and value is the y axis, and the part I'm struggling to do is, for the rows that have flag=0 I want the color of this point to be blue and similarly if flag=1 I want to color the point red.

Is there a simple to to check the flag column per row and color the point accordingly?

like image 334
Teodorico Levoff Avatar asked Apr 06 '18 01:04

Teodorico Levoff


1 Answers

You can use the built-in df.plot.scatter() function and pass the flag column to the color argument:

import pandas as pd

idx = [1,2,3]
value = [0., 0.03, 0.13]
flag = [0, 0, 1]

df = pd.DataFrame(dict(idx=idx, value=value, flag=flag))

df.plot.scatter('idx', 'value', c='flag', cmap='RdBu_r')

enter image description here

like image 98
Daniel Lenz Avatar answered Oct 09 '22 01:10

Daniel Lenz