Coding/Python

Seaborn 라이브러리 모음

폴밴 2022. 2. 23. 11:09
import seaborn as sns
%matplotlib inline

Load Data

tips = sns.load_dataset('tips')
tips.head()

Distribution Plot

# distribution plot
sns.displot(tips['total_bill
# kde
sns.distplot(tips['total_bill'], kde = True, bins = 30)

Joint Plot

# kind = plot 종류
sns.jointplot(x = 'total_bill', y = 'tip', data = tips, kind = 'scatter')
sns.jointplot(x = 'total_bill', y = 'tip', data = tips, kind = 'hex')
sns.jointplot(x = 'total_bill', y = 'tip', data = tips, kind = 'reg')

Pair Plot

sns.pairplot(data = tips)
sns.pairplot(data = tips, hue = 'sex', palette = 'coolwarm')

Other Plots

sns.rugplot(tips['total_bill'])
sns.kdeplot(tips['total_bill'])

Categorical Dataplots

sns.barplot(x = 'sex', y = 'total_bill', data = tips)  # default : mean

import numpy as np
# change estimator to standard deviation
sns.barplot(x = 'sex', y = 'total_bill', data = tips, estimator = np.std)
# countplot
sns.countplot(x='time', data = tips)

# boxplot
sns.boxplot(x = 'day', y = 'total_bill', data = tips, hue = 'sex', palette = 'rainbow')
sns.boxplot(data = tips[['total_bill','tip']], palette = 'coolwarm', orient='h')

# violin plot
sns.violinplot(x = 'day', y= 'total_bill', data = tips, 
               hue = 'sex',split = True, palette = 'Set1')

#stripplot
sns.stripplot(x = 'day', y= 'total_bill', data = tips)
sns.stripplot(x = 'day', y= 'total_bill', data = tips, jitter = False)
sns.stripplot(x = 'day', y= 'total_bill', data = tips, 
              hue = 'sex', palette = 'Set1', split = True)

#swarmplot
sns.swarmplot(x = 'day', y= 'total_bill', data = tips, 
              hue = 'sex', split = True, palette = 'Set1')

# override plots
sns.stripplot(x = 'day', y = 'total_bill', data = tips, color = 'white')
sns.violinplot(x = 'day', y = 'total_bill', data = tips)
# use kind parameter to choose plot type

sns.factorplot(x = 'sex', y='tip', data = tips, kind = 'bar')

Heatmap

flights = sns.load_dataset('flights')
tips = sns.load_dataset('tips')
flights.head()
tips.head()

# Make Matrix with Correlation Data
tips.corr()

# Make Heatmap
sns.heatmap(tips.corr(), cmap = 'coolwarm', annot = True)
# Make Matrix with Pivot Table
pvflights = flights.pivot_table(values = 'passengers', index='month', columns='year')

# Make Heatmap
sns.heatmap(data = pvflights, linecolor = 'white', linewidths = 1) # set lines
# hierarchal clustering
sns.clustermap(pvflights, cmap = 'coolwarm', standard_scale=1)

Grids

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

# Make grid
sns.PairGrid(iris)  

# map to the grid
import matplotlib.pyplot as plt
g = sns.PairGrid(iris)
g.map(plt.scatter)
g = sns.PairGrid(iris)
g.map_diag(plt.hist)  # set diagonal plots to histplot
g.map_upper(plt.scatter)  # set diagonal plots to scatterplot
g.map_lower(sns.kdeplot)  # set diagonal plots to kdeplot

sns.pairplot(iris, hue = 'species', palette = 'rainbow')

Facet Grid

g = sns.FacetGrid(tips, col = 'time', row = 'smoker')  # make grid
g = g.map(plt.hist, 'total_bill')
g = sns.JointGrid(x = 'total_bill', y = 'tip', data = tips)
g = g.plot(sns.regplot, sns.distplot) # regplot for mainplot, displot for 2 subplots

Joint Grid (Lmplot)

# lmplot allows linear models to be displayed

# markers, markers size(kws)
sns.lmplot(x = 'total_bill', y = 'tip', data = tips, hue = 'sex', 
           palette = 'coolwarm', markers = ['o','v'], scatter_kws={'s':100})  
           
sns.lmplot(x = 'total_bill', y = 'tip', data = tips, col = 'time', row = 'sex')
# aspect and size
sns.lmplot(x = 'total_bill', y = 'tip', data = tips, 
           col = 'day', hue = 'sex', palette = 'coolwarm',
           aspect = 0.6, size = 8)

Styles and Color

sns.set_style('ticks')   # set background style
sns.countplot(x = 'sex', data = tips)
sns.despine(left = True)  # remove spine

Scale

sns.set_context('poster', font_scale = 2)
sns.countplot(x='sex', data = tips, palette = 'Set1')

'Coding > Python' 카테고리의 다른 글

Plotly & Cofflinks 라이브러리 모음  (0) 2022.02.25
Pandas Built-in Data Visualization 모음  (0) 2022.02.23
Matplotlib 라이브러리 모음  (0) 2022.02.22
Pandas 라이브러리 모음  (0) 2022.02.21
Numpy 라이브러리 모음  (0) 2022.02.21