0% found this document useful (0 votes)
245 views19 pages

Python Seaborn Tutorial - Jupyter Notebook

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 19

6/6/22, 6:13 PM Python Seaborn Tutorial - Jupyter Notebook

In [1]:

import seaborn as sns


import matplotlib.pyplot as plt

In [2]:

#To get the current version of seaborn


print(sns.__version__)

0.11.0

In [3]:

iris_df = sns.load_dataset('iris')
iris_df.head()

Out[3]:

sepal_length sepal_width petal_length petal_width species

0 5.1 3.5 1.4 0.2 setosa

1 4.9 3.0 1.4 0.2 setosa

2 4.7 3.2 1.3 0.2 setosa

3 4.6 3.1 1.5 0.2 setosa

4 5.0 3.6 1.4 0.2 setosa

In [4]:

iris_df['species'].unique()

Out[4]:

array(['setosa', 'versicolor', 'virginica'], dtype=object)

localhost:8888/notebooks/Python Seaborn Tutorial.ipynb 1/19


6/6/22, 6:13 PM Python Seaborn Tutorial - Jupyter Notebook

In [5]:

sns.scatterplot(x='sepal_length',y='sepal_width',hue='species',data=iris_df)
plt.show()

In [6]:

sns.scatterplot(x='petal_length',y='petal_width',hue='species',data=iris_df)
plt.show()

localhost:8888/notebooks/Python Seaborn Tutorial.ipynb 2/19


6/6/22, 6:13 PM Python Seaborn Tutorial - Jupyter Notebook

In [7]:

df=sns.load_dataset("fmri")
df.head()

Out[7]:

subject timepoint event region signal

0 s13 18 stim parietal -0.017552

1 s5 14 stim parietal -0.080883

2 s12 18 stim parietal -0.081033

3 s11 18 stim parietal -0.046134

4 s10 18 stim parietal -0.037970

In [8]:

df.shape

Out[8]:

(1064, 5)

In [9]:

stim = df[df['event']=='stim']
cue = df[df['event']=='cue']

In [10]:

stim.head()

Out[10]:

subject timepoint event region signal

0 s13 18 stim parietal -0.017552

1 s5 14 stim parietal -0.080883

2 s12 18 stim parietal -0.081033

3 s11 18 stim parietal -0.046134

4 s10 18 stim parietal -0.037970

localhost:8888/notebooks/Python Seaborn Tutorial.ipynb 3/19


6/6/22, 6:13 PM Python Seaborn Tutorial - Jupyter Notebook

In [11]:

cue.head()

Out[11]:

subject timepoint event region signal

532 s3 4 cue parietal 0.058219

533 s6 5 cue parietal 0.038145

534 s7 5 cue parietal -0.008158

535 s8 5 cue parietal 0.047136

536 s9 5 cue parietal 0.055847

In [13]:

df.event.unique()

Out[13]:

array(['stim', 'cue'], dtype=object)

In [14]:

df['region'].unique()

Out[14]:

array(['parietal', 'frontal'], dtype=object)

In [15]:

sns.lineplot(x="timepoint",y="signal",data=df)
plt.title('My LinePlot')
plt.show()

localhost:8888/notebooks/Python Seaborn Tutorial.ipynb 4/19


6/6/22, 6:13 PM Python Seaborn Tutorial - Jupyter Notebook

In [16]:

sns.lineplot(x="timepoint",y="signal",data=df,hue="event")
plt.show()

In [17]:

sns.lineplot(x="timepoint",y="signal",data=df,hue="event",style="event",markers=True)
plt.show()

localhost:8888/notebooks/Python Seaborn Tutorial.ipynb 5/19


6/6/22, 6:13 PM Python Seaborn Tutorial - Jupyter Notebook

In [18]:

import pandas as pd
df1=pd.read_csv("iris.csv")
df1.head()

Out[18]:

sepal_length sepal_width petal_length petal_width species

0 5.1 3.5 1.4 0.2 Iris-setosa

1 4.9 3.0 1.4 0.2 Iris-setosa

2 4.7 3.2 1.3 0.2 Iris-setosa

3 4.6 3.1 1.5 0.2 Iris-setosa

4 5.0 3.6 1.4 0.2 Iris-setosa

In [19]:

df1['species'].unique()

Out[19]:

array(['Iris-setosa', 'Iris-versicolor', 'Iris-virginica'], dtype=object)

In [20]:

sns.distplot(df1['sepal_length'])
plt.grid(True)
plt.show()

C:\ProgramData\Anaconda3\lib\site-packages\seaborn\distributions.py:2551: Fu
tureWarning: `distplot` is a deprecated function and will be removed in a fu
ture version. Please adapt your code to use either `displot` (a figure-level
function with similar flexibility) or `histplot` (an axes-level function for
histograms).

warnings.warn(msg, FutureWarning)

localhost:8888/notebooks/Python Seaborn Tutorial.ipynb 6/19


6/6/22, 6:13 PM Python Seaborn Tutorial - Jupyter Notebook

In [21]:

df1.species.unique()

Out[21]:

array(['Iris-setosa', 'Iris-versicolor', 'Iris-virginica'], dtype=object)

In [22]:

sns.pairplot(data=df1,hue='species')
plt.show()

localhost:8888/notebooks/Python Seaborn Tutorial.ipynb 7/19


6/6/22, 6:13 PM Python Seaborn Tutorial - Jupyter Notebook

In [23]:

sns.scatterplot(x="sepal_length",y="petal_width",data=df1,hue="species",style="species")
plt.show()

In [24]:

sns.swarmplot(x="species",y="petal_length",data=df1)
plt.grid(True)
plt.show()

C:\ProgramData\Anaconda3\lib\site-packages\seaborn\categorical.py:1296: User
Warning: 14.0% of the points cannot be placed; you may want to decrease the
size of the markers or use stripplot.

warnings.warn(msg, UserWarning)

localhost:8888/notebooks/Python Seaborn Tutorial.ipynb 8/19


6/6/22, 6:13 PM Python Seaborn Tutorial - Jupyter Notebook

In [25]:

df2=sns.load_dataset("flights")
df2.head()

Out[25]:

year month passengers

0 1949 Jan 112

1 1949 Feb 118

2 1949 Mar 132

3 1949 Apr 129

4 1949 May 121

In [26]:

df2.shape

Out[26]:

(144, 3)

In [27]:

sns.relplot(x="passengers",y="month",data=df2)
plt.show()

localhost:8888/notebooks/Python Seaborn Tutorial.ipynb 9/19


6/6/22, 6:13 PM Python Seaborn Tutorial - Jupyter Notebook

In [28]:

sns.set_style('darkgrid')
sns.color_palette("tab10")
df3=sns.load_dataset("flights")
sns.relplot(x="passengers",y="month",hue="year",data=df3)

plt.show()

In [29]:

df4=sns.load_dataset("tips")
df4.head()

Out[29]:

total_bill tip sex smoker day time size

0 16.99 1.01 Female No Sun Dinner 2

1 10.34 1.66 Male No Sun Dinner 3

2 21.01 3.50 Male No Sun Dinner 3

3 23.68 3.31 Male No Sun Dinner 2

4 24.59 3.61 Female No Sun Dinner 4

localhost:8888/notebooks/Python Seaborn Tutorial.ipynb 10/19


6/6/22, 6:13 PM Python Seaborn Tutorial - Jupyter Notebook

In [30]:

df4.time.unique()

Out[30]:

['Dinner', 'Lunch']

Categories (2, object): ['Dinner', 'Lunch']

In [31]:

sns.boxplot(df4.total_bill)
plt.grid(True)
plt.show()

C:\ProgramData\Anaconda3\lib\site-packages\seaborn\_decorators.py:36: Future
Warning: Pass the following variable as a keyword arg: x. From version 0.12,
the only valid positional argument will be `data`, and passing other argumen
ts without an explicit keyword will result in an error or misinterpretation.

warnings.warn(

localhost:8888/notebooks/Python Seaborn Tutorial.ipynb 11/19


6/6/22, 6:13 PM Python Seaborn Tutorial - Jupyter Notebook

In [32]:

sns.relplot(x="total_bill",y="tip",hue="time",data=df4)
plt.show()

In [34]:

sns.relplot(x="time",y="tip",kind="line",data=df4)
plt.show()

localhost:8888/notebooks/Python Seaborn Tutorial.ipynb 12/19


6/6/22, 6:13 PM Python Seaborn Tutorial - Jupyter Notebook

In [35]:

sns.catplot(x=df4["day"],y=df4["total_bill"],data=df4)
plt.show()

In [36]:

sns.catplot(x="day",y="total_bill",kind="boxen",data=df4)
plt.show()

localhost:8888/notebooks/Python Seaborn Tutorial.ipynb 13/19


6/6/22, 6:13 PM Python Seaborn Tutorial - Jupyter Notebook

In [37]:

sns.catplot(x="day",y="total_bill",kind="violin",data=df4)
plt.show()

In [38]:

sns.boxplot(x="day",y="total_bill",data=df4)
plt.show()

localhost:8888/notebooks/Python Seaborn Tutorial.ipynb 14/19


6/6/22, 6:13 PM Python Seaborn Tutorial - Jupyter Notebook

In [39]:

a=sns.load_dataset("iris")

b=sns.FacetGrid(a,col="species")
b.map(plt.hist,"sepal_length")

plt.show()

localhost:8888/notebooks/Python Seaborn Tutorial.ipynb 15/19


6/6/22, 6:13 PM Python Seaborn Tutorial - Jupyter Notebook

In [40]:

a=sns.load_dataset("flights")
b=sns.PairGrid(a)
b.map(plt.scatter)

plt.show()

In [46]:

#ColorPalette
c=sns.color_palette()
sns.palplot(c)

localhost:8888/notebooks/Python Seaborn Tutorial.ipynb 16/19


6/6/22, 6:13 PM Python Seaborn Tutorial - Jupyter Notebook

In [47]:

l = [0, 1, 2, 3, 4, 5]
sns.distplot(l)

plt.show()

C:\ProgramData\Anaconda3\lib\site-packages\seaborn\distributions.py:2551: Fu
tureWarning: `distplot` is a deprecated function and will be removed in a fu
ture version. Please adapt your code to use either `displot` (a figure-level
function with similar flexibility) or `histplot` (an axes-level function for
histograms).

warnings.warn(msg, FutureWarning)

localhost:8888/notebooks/Python Seaborn Tutorial.ipynb 17/19


6/6/22, 6:13 PM Python Seaborn Tutorial - Jupyter Notebook

In [48]:

sns.distplot([0, 1, 2, 3, 4, 5], hist=False)

plt.show()

C:\ProgramData\Anaconda3\lib\site-packages\seaborn\distributions.py:2551: Fu
tureWarning: `distplot` is a deprecated function and will be removed in a fu
ture version. Please adapt your code to use either `displot` (a figure-level
function with similar flexibility) or `kdeplot` (an axes-level function for
kernel density plots).

warnings.warn(msg, FutureWarning)

In [49]:

from numpy import random

x = random.binomial(n=10, p=0.5, size=10)

print(x)

[7 7 7 4 7 6 3 5 5 3]

localhost:8888/notebooks/Python Seaborn Tutorial.ipynb 18/19


6/6/22, 6:13 PM Python Seaborn Tutorial - Jupyter Notebook

In [50]:

sns.distplot(random.binomial(n=10, p=0.5, size=1000),hist=False)

plt.show()

C:\ProgramData\Anaconda3\lib\site-packages\seaborn\distributions.py:2551: Fu
tureWarning: `distplot` is a deprecated function and will be removed in a fu
ture version. Please adapt your code to use either `displot` (a figure-level
function with similar flexibility) or `kdeplot` (an axes-level function for
kernel density plots).

warnings.warn(msg, FutureWarning)

In [ ]:

localhost:8888/notebooks/Python Seaborn Tutorial.ipynb 19/19

You might also like