Plotting with Seaborn: Part 1

keshav
Analytics Vidhya
Published in
3 min readJan 24, 2021

--

Seaborn is a library for making statistical graphics in Python. It builds on top of matplotlib and integrates closely with pandas data structures.

Seaborn helps you explore and understand your data. Its plotting functions operate on dataframes and arrays containing whole datasets and internally perform the necessary semantic mapping and statistical aggregation to produce informative plots. Its dataset-oriented, declarative API lets you focus on what the different elements of your plots mean, rather than on the details of how to draw them.

seaborn.get_dataset_names()

Report available example datasets, useful for reporting issues.

seaborn.load_dataset(name, cache=True, data_home=None, **kws)

Load an example dataset from the online repository. This function provides quick access to a small number of example datasets that are useful for documenting seaborn.

We will be using “tips” dataSet.

d = sns.load_dataset(“tips”)

Kdeplot

fig,ax = plt.subplots(figsize =(10,10))
sns.kdeplot(data=d,fill=True)

Now, we have plotted a kdeplot, where the are enclosed is given fill color to make it readable.

We can hue mapping,

sns.kdeplot(data=d, x=”total_bill”, hue=”sex”)

We can stack the distribution,

sns.kdeplot(data=d, x="total_bill", hue="sex",multiple="stack")

We could modify the appearance,

sns.kdeplot(
data=d, x=”total_bill”, hue=”size”,
fill=True, common_norm=False, palette=”crest”,
alpha=.5, linewidth=0,)

Jointplot

Draw a plot of two variables with bivariate and univariate graphs. This function provides a convenient interface to the JointGrid class, with several canned plot kinds.

seaborn.jointplot(*, x=None, y=None, data=None, kind='scatter', color=None, height=6, ratio=5, space=0.2, dropna=False, xlim=None, ylim=None, marginal_ticks=False, joint_kws=None, marginal_kws=None, hue=None, palette=None, hue_order=None, hue_norm=None, **kwargs)

Now to plot,

sns.jointplot(data=d)

Now to use hue mapping,

sns.jointplot(data=d,x=”size”,y=”tip”,hue=”sex”)
sns.jointplot(data=d,x=”total_bill”,y=”tip”,hue=”sex”)

Several different approaches to plotting are available through the kind parameter,

sns.jointplot(data=d,x=”total_bill”,y=”tip”,kind=”reg”)
sns.jointplot(data=d,x="total_bill",y="tip",kind="hist")
sns.jointplot(data=d,x="total_bill",y="tip",kind="hex")

Now to use marker,

sns.jointplot(data=d,x=”total_bill”,y=”tip”,marker=”*”,hue=”time”)

JointGrid

Grid for drawing a bivariate plot with marginal univariate plots.

The simplest plotting method, JointGrid.plot() accepts a pair of functions

g = sns.JointGrid(data=d, x="size", y="tip",hue="sex")g.plot(sns.scatterplot, sns.histplot, alpha=.7, edgecolor=".2", linewidth=.5)

Now we can create a function to create the joint grid.

def jointer(x,y,z):
g = sns.JointGrid(data=d, x=x, y=y,hue=z)
return g

Now we can use the function and plot graphs.

g = jointer("tip","total_bill","day")
g.plot_joint(sns.scatterplot, s=100, alpha=.5)
g.plot_marginals(sns.histplot, kde=True)

--

--