6

I have a pandas dataframe:

--------------------------------------
|   field_0  |  field_1   | field_2  |
--------------------------------------
|     0      |   1.5      |  2.9     |
--------------------------------------
|     1      |   1.3      |  2.6     |
--------------------------------------
|       :                            |
--------------------------------------
|     1      |   2.1      |  3.2     |
--------------------------------------
|     0      |   1.1      |  2.1     |
--------------------------------------      

I can create a scatter plot for field_1 vs. field_2 like below:

%matplotlib notebook
import matplotlib.pyplot as plt
import matplotlib
matplotlib.style.use('ggplot')
my_df.plot(x='field_1', y='field_2', kind = 'scatter')

However, I am wondering is it possible to include field_0 in the plot? So when field_0 = 0, the point is blue; when field_1 = 1, the point is red.

Thanks!

Edamame
  • 23,718
  • 73
  • 186
  • 320
  • Possible duplicate of [matplotlib: how to change data points color based on some variable](http://stackoverflow.com/questions/7881994/matplotlib-how-to-change-data-points-color-based-on-some-variable) – LinkBerest Sep 18 '16 at 18:50

1 Answers1

18

you can do it this way:

col = df.field_0.map({0:'b', 1:'r'})
df.plot.scatter(x='field_1', y='field_2', c=col)

Result:

enter image description here

MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419