Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple data in scatter matrix

Is it possible to add multiple data to a pandas.tools.plotting.scatter_matrix and assigning a color to each group of data?

I'd like to show the scatter plots with data points for one group of data, let's say, in green and the other group in red in the very same scatter matrix. The same should apply for the density plots on the diagonal. I know that this is possible by using matplotlib's scatter function, but that does not give me a scatter matrix.

The documentation of pandas is mum on that.

like image 847
Manuel Avatar asked Jan 15 '14 07:01

Manuel


People also ask

How do you group data in a scatter plot?

If you have a grouping variable you can create a scatter plot by group passing the variable (as factor) to the col argument of the plot function, so each group will be displayed with a different color.

What does a scatter matrix tell you?

A scatter plot matrix is a grid (or matrix) of scatter plots used to visualize bivariate relationships between combinations of variables. Each scatter plot in the matrix visualizes the relationship between a pair of variables, allowing many relationships to be explored in one chart.


2 Answers

The short answer is determine the color of each dot in the scatter plot, role it into an array and pass it as the color argument.

Example:

from pandas.tools.plotting import scatter_matrix
import pandas as pd
from sklearn import datasets

iris = datasets.load_iris()
iris_data = pd.DataFrame(data=iris['data'],columns=iris['feature_names'])
iris_data["target"] = iris['target']

color_wheel = {1: "#0392cf", 
               2: "#7bc043", 
               3: "#ee4035"}
colors = iris_data["target"].map(lambda x: color_wheel.get(x + 1))
ax = scatter_matrix(iris_data, color=colors, alpha=0.6, figsize=(15, 15), diagonal='hist')

Iris Dataset

like image 125
neone4373 Avatar answered Oct 08 '22 21:10

neone4373


For me this answer didn't worked... But with this little correction it went well for me !

import pandas as pd
from pandas.plotting import scatter_matrix
from sklearn import datasets

iris = datasets.load_iris()
iris_data = pd.DataFrame(data=iris['data'],columns=iris['feature_names'])
iris_data["target"] = iris['target']

color_wheel = {1: "#0392cf", 
               2: "#7bc043", 
               3: "#ee4035"}
colors = iris_data["target"].map(lambda x: color_wheel.get(x + 1))
ax = scatter_matrix(iris_data, color=colors, alpha=0.6, figsize=(15, 15), diagonal='hist')
like image 40
cmoi4lec Avatar answered Oct 08 '22 20:10

cmoi4lec