Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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,

like image 240
user3446905 Avatar asked Sep 29 '18 15:09

user3446905


People also ask

Can seaborn generate graphics plot?

Seaborn creates complete graphics with a single function call: when possible, its functions will automatically add informative axis labels and legends that explain the semantic mappings in the plot.

Is seaborn better than Matplotlib?

Seaborn vs matplotlib is that seaborn utilises fascinating themes, while matplotlib used for making basic graphs. Seaborn contains a few plots and patterns for data visualisation, while in matplotlib, datasets are visualised with the assistance of lines, scatter plots, pie charts, histograms, bar-graphs, etc.

How do you plot multiple graphs in Python using seaborn?

In Seaborn, we will plot multiple graphs in a single window in two ways. First with the help of Facetgrid() function and other by implicit with the help of matplotlib. data: Tidy dataframe where each column is a variable and each row is an observation.


1 Answers

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

like image 178
devssh Avatar answered Oct 26 '22 20:10

devssh