user3446905
user3446905

Reputation: 316

Plotting numpy array using Seaborn

I'm using python 2.7. I know this will be very basic, however I'm really confused and I would like to have a better understanding of seaborn.

I have two numpy arrays X and y and I'd like to use Seaborn to plot them.

Here is my X numpy array:

[[ 1.82716998 -1.75449225]
 [ 0.09258069  0.16245259]
 [ 1.09240926  0.08617436]]

And here is the y numpy array:

[ 1. -1.  1. ]

How can I successfully plot my data points taking into account the class label from the y array?

Thank you,

Upvotes: 13

Views: 52879

Answers (1)

devssh
devssh

Reputation: 1186

You can use seaborn functions to plot graphs. Do dir(sns) to see all the plots. Here is your output in sns.scatterplot. You can check the api docs here or example code with plots here

import seaborn as sns 
import pandas as pd

df = pd.DataFrame([[ 1.82716998, -1.75449225],
 [ 0.09258069,  0.16245259],
 [ 1.09240926,  0.08617436]], columns=["x", "y"])

df["val"] = pd.Series([1, -1, 1]).apply(lambda x: "red" if x==1 else "blue")


sns.scatterplot(df["x"], df["y"], c=df["val"]).plot()

Gives

enter image description here Is this the exact input output you wanted?

You can do it with pyplot, just importing seaborn changes pyplot color and plot scheme

import seaborn as sns 

import matplotlib.pyplot as plt
fig, ax = plt.subplots()

df = pd.DataFrame([[ 1.82716998, -1.75449225],
 [ 0.09258069,  0.16245259],
 [ 1.09240926,  0.08617436]], columns=["x", "y"])
df["val"] = pd.Series([1, -1, 1]).apply(lambda x: "red" if x==1 else "blue")
ax.scatter(x=df["x"], y=df["y"], c=df["val"])
plt.plot()

Here is a stackoverflow post of doing the same with sns.lmplot

Upvotes: 12

Related Questions