Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python scatter plot different colors depending on value

I have a dataframe which i want to make a scatter plot of.

the dataframe looks like:

       year  length  Animation
0      1971     121       1
1      1939      71       1
2      1941       7       0
3      1996      70       1
4      1975      71       0

I want the points in my scatter plot to be a different color depending the value in the Animation row.
So animation = 1 = yellow
animation = 0 = black
or something similiar

I tried doing the following:

dfScat = df[['year','length', 'Animation']]
dfScat = dfScat.loc[dfScat.length < 200]    
axScat = dfScat.plot(kind='scatter', x=0, y=1, alpha=1/15, c=2)

This results in a slider which makes it hard to tell the difference. enter image description here

like image 675
Rainoa Avatar asked Apr 18 '17 21:04

Rainoa


People also ask

Is it possible to create a colored scatter plot using Matplotlib?

Use matplotlib. pyplot. scatter() to make a colored scatter plot. Using lists of corresponding x and y coordinates and a list colors , create a list color_indices that assigns each coordinate the index of a color from colors .


2 Answers

You can also assign discrete colors to the points by passing an array to c= Like this:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

d = {"year"      : (1971, 1939, 1941, 1996, 1975),
     "length"    : ( 121,   71,    7,   70,   71),
     "Animation" : (   1,    1,    0,    1,    0)}

df = pd.DataFrame(d)
print(df)

colors = np.where(df["Animation"]==1,'y','k')
df.plot.scatter(x="year",y="length",c=colors)
plt.show()

This gives:

   Animation  length  year
0          1     121  1971
1          1      71  1939
2          0       7  1941
3          1      70  1996
4          0      71  1975

enter image description here

like image 137
Arjaan Buijk Avatar answered Sep 20 '22 15:09

Arjaan Buijk


Use the c parameter in scatter

df.plot.scatter('year', 'length', c='Animation', colormap='jet')

enter image description here

like image 25
piRSquared Avatar answered Sep 20 '22 15:09

piRSquared