13

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,

user3446905
  • 316
  • 1
  • 2
  • 13
  • Can you also add how you want the X & Y to be correlated and what X means? X array has shape of 2 x 3 values. These 2 things in array are x, y coordinates? How to display the Y values? Would you like a scatter plot? – devssh Sep 29 '18 at 16:03

1 Answers1

12

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

devssh
  • 1,184
  • 12
  • 28
  • Thank you this worked. I think it does what I want now it's my task to go back to the features and investigate. Thank you!! The second example causes this error: AttributeError: 'module' object has no attribute 'scatterplot' – user3446905 Sep 29 '18 at 16:54
  • Please note that my data don't have commas as seperators – user3446905 Sep 29 '18 at 16:56
  • Just use X and Y instead of the arrays, no need for comma as seperators, pd.DataFrame has option to set delimiter. – devssh Sep 29 '18 at 16:58
  • sns.scatterplot is a valid function, I added the api doc for it. – devssh Sep 29 '18 at 17:03
  • it looks like it's a new feature added in 0.9.0. Can you give an example of sns.(function) from 0.8.0. I tried the other post solution it gave TypeError: lmplot() got an unexpected keyword argument 'c' – user3446905 Sep 29 '18 at 17:13
  • Here is the api of lmplot from seaborn 0.8.1 https://web.archive.org/web/20171230053852/http://seaborn.pydata.org:80/generated/seaborn.lmplot.html#seaborn.lmplot It has examples of using the data attribute directly to color the plot using the DataFrame. See `g = sns.lmplot(x="total_bill", y="tip", hue="smoker", data=tips,palette=dict(Yes="g", No="m"))` If you use the pyplot version of my answer, you will be able to plot on `ax` object multiple times. So you can plot scatter once with a dataframe view using df.loc[predicate, "column"] in one color, and plot on it again with another color. – devssh Sep 29 '18 at 18:31