In [2]:
"""
A simple example of an animated plot... In 3D!
"""
%matplotlib inline
from sklearn.datasets import load_iris
import numpy as np
import matplotlib.pyplot as plt
In [3]:
data = load_iris()
In [4]:
from pandas import DataFrame, Series
import pandas as pd
In [11]:
df = DataFrame(data.data)
In [12]:
df.head()
Out[12]:
In [13]:
df.columns = ['a','b','c','d']
In [14]:
target = Series(data.target)
In [15]:
target
Out[15]:
In [28]:
plt.figure(1)
colors = {1:'r',0:'b',2:'g'}
for i in target.unique():
mask = target == i
plt.scatter(df['a'][mask], df['b'][mask], c = colors[i], label = i)
plt.xlabel("X")
plt.ylabel("Y")
plt.legend()
plt.show()
In [29]:
def scatter_df(df, x_name, y_name, groupby, colour_dict, size=(10,10)):
"""
df - DataFrame with columns name
x_name - the x column name
y_name - the y column name
groupby - the categorical column Series
size - the plot size
"""
plt.figure(1, figsize=size)
for item in groupby.unique():
mask_s = groupby == item
plt.scatter(df[x_name][mask], df[y_name][mask], c = colour_dict[item], label = item)
plt.xlabel(x_name)
plt.ylabel(y_name)
plt.legend()
plt.show
In [32]:
test_df = DataFrame(np.random.rand(500,2))
In [33]:
test_df.head()
Out[33]:
In [39]:
t = np.random.random_integers(5, size=(500))
In [41]:
color = {1:'r',2:'b',3:'g',4:'k',5:'y'}
In [49]:
group = Series(t)
In [43]:
test_df.columns = ['x_value','y_value']
In [45]:
scatter_df(test_df, 'x_value','y_value',group,color)
In [46]:
group.shape
Out[46]:
In [47]:
test_df.shape
Out[47]:
In [50]:
group == 1
Out[50]:
In [ ]: