> AI Gravity :: Seaborn Library
본문으로 바로가기

Seaborn Library

category AI Library/Pandas 2021. 11. 14. 22:19

1. Univariate graph(단변량 그래프)

Code:

import seaborn as sns
tips=sns.load_dataset("tips")
ax=plt.subplots()						#one graph in one figure
ax=sns.distplot(tips['total_bill'])
ax.set_title('Total Bill Histogram with Density Plot')

#without kde
ax=plt.subplots()
ax=sns.distplot(tips['total_bill'],kde=False)       
ax.set_title('Total Bill Histogram')
ax.set_xlabel('Total Bill')
ax.set_ylabel('Frequency')

#without histogram
ax=plt.subplots()
ax=sns.distplot(tips['total_bill'],hist=False)       
ax.set_title('Total Bill Histogram')
ax.set_xlabel('Total Bill')
ax.set_ylabel('Frequency')

#with rug
ax=plt.subplots()
ax=sns.distplot(tips['total_bill'],rug=True)
ax.set_title('Total Bill Histogram with Density and Rug Plot')
ax.set_xlabel('Total Bill')

#use countplot
ax=plt.subplots()
ax=sns.countplot('day',data=tips)
ax.set_title('Count of days')
ax.set_xlabel('Day of the Week')
ax.set_ylabel('Frequency')

Result:

2. Bivariate Graph(이변량 그래프)

Code:

import seaborn as sns
import matplotlib.pyplot as plt
tips=sns.load_dataset("tips")

#use seaborn lib
ax=plt.subplots()
ax=sns.regplot(x='total_bill',y='tip',data=tips)
ax.set_title('Scatterplot of Total Bill vs Tip')
ax.set_xlabel('Total Bill')
ax.set_ylabel('Tip')

#no line
ax=plt.subplots()
ax=sns.regplot(x='total_bill',y='tip',data=tips,fit_reg=False)
ax.set_title('Scatterplot of Total Bill vs Tip')
ax.set_xlabel('Total Bill')
ax.set_ylabel('Tip')

#Many graph driven
scatter_plot=plt.figure()
ax=scatter_plot.add_subplot(1,1,1)
ax.scatter(tips['total_bill'],tips['tip'])
ax.set_title('Scatterplot of Total Bill vs Tip')
ax.set_xlabel('Total Bill')
ax.set_ylabel('Tips')

#scatter & histogram
joint=sns.jointplot(x='total_bill',y='tip',data=tips)
joint.set_axis_labels(xlabel='Total Bill',ylabel='Tip')
joint.fig.suptitle('Joint Plot of Total Bill and Tip',fontsize=10,y=1.03)

#scatter & histogram as hexbin
joint=sns.jointplot(x='total_bill',y='tip',data=tips,kind="hex")
joint.set_axis_labels(xlabel='Total Bill',ylabel='Tip')
joint.fig.suptitle('Joint Plot of Total Bill and Tip',fontsize=10,y=1.03)

#Kernel Density Plot
ax=plt.subplots()
ax=sns.kdeplot(data=tips['total_bill'],
               data2=tips['tip'],
               shade=True)
ax.set_title('Kernel Density Plot of Total Bill and Tip')
ax.set_xlabel('Total Bill')
ax.set_ylabel('Tip')

#barplot
ax=plt.subplots()
ax=sns.barplot(x='time',y='total_bill',data=tips)
ax.set_title('Bar plot of average total bill for time of day')
ax.set_xlabel('Time of day')
ax.set_ylabel('Average total bill')

#boxplot
ax=plt.subplots()
ax=sns.boxplot(x='time',y='total_bill',data=tips)
ax.set_title('Boxplot of total bill for time of day')
ax.set_xlabel('Time of day')
ax.set_ylabel('Total bill')

#violin plot
ax=plt.subplots()
ax=sns.violinplot(x='time',y='total_bill',data=tips)
ax.set_title('Boxplot of total bill for time of day')
ax.set_xlabel('Time of day')
ax.set_ylabel('Total bill')

#pair plot (관계 그래프)
fig = sns .pairplot(tips)

#pair plot change (관계 그래프 지정)
pair_grid=sns.PairGrid(tips)
pair_grid=pair_grid.map_upper(sns.regplot)
pair_grid=pair_grid.map_lower(sns.kdeplot)
pair_grid=pair_grid.map_diag(sns.distplot,rug=True)
plt.show()

Result:

 

3. Multivariate Graph(다변량 그래프)

#violin plot with seaborn lib - add color
ax=plt.subplots()
ax=sns.violinplot(x='time',y='total_bill',hue='sex',data=tips,split=True)

#lmplot = scatter & pair
scatter=sns.lmplot(x='total_bill',y='tip',data=tips,hue='sex',fit_reg=False)

#pairplot + sex
fig = sns.pairplot(tips,hue='sex')

#scatter = size,shape
#scatter=sns.lmplot(x='total_bill',y='tip',data=tips,fit_reg=False,hue='sex',scatter_kws={'s' : tips['size']*10})

#different mark
#scatter=sns.lmplot(x='total_bill',y='tip',data=tips,fit_reg=False,hue='sex',marker=['o','x'],scatter_kws={'s' : tips['size']*10})

#lmplot으로 4개 데이터 그룹 한번에 그리기
anscombe=sns.load_dataset("anscombe")
anscombe_plot=sns.lmplot(x='x',y='y',data=anscombe,fit_reg=False)

#lmplot으로 4개 데이터 그룹 나누어 그리기
anscombe_plot=sns.lmplot(x='x',y='y',data=anscombe,fit_reg=False,col='dataset',col_wrap=2)

Result:

 

'AI Library > Pandas' 카테고리의 다른 글

Pandas DataFrame and Series  (0) 2021.11.15
Graph Type  (0) 2021.11.15
Matplotlib  (0) 2021.11.14
Anscombe's quartet  (0) 2021.11.14
Data Extract  (0) 2021.11.12