본문 바로가기
Data Science/EDA Practice

[EDA Practice] Seaborn 설정 (rc)

by 인사이티드 2023. 6. 23.

데이터 시각화에 대해서 더 공부하다가 rc 설정을 알게 되었다. rc 설정은 사용자가 그래프 스타일을 커스터마이징할 수 있도록 하는 파라미터이다. 지금까지는 시각화를 위해서 적절한 그래프와 데이터를 선택했다면 rc 설정은 그래프 자체에 대한 스타일을 설정하여 결과적으로 가독성을 높여준다.

sns.axes_style()을 통해 현재 그래프 스타일 설정을 확인해볼 수 있다.

import seaborn as sns

sns.axes_style()
=>
{'axes.facecolor': '#EAEAF2',
 'axes.edgecolor': 'white',
 'axes.grid': True,
 'axes.axisbelow': True,
 'axes.labelcolor': '.15',
 'figure.facecolor': 'white',
 'grid.color': 'white',
 'grid.linestyle': '-',
 'text.color': '.15',
 'xtick.color': '.15',
 'ytick.color': '.15',
 'xtick.direction': 'out',
 'ytick.direction': 'out',
 'lines.solid_capstyle': <CapStyle.round: 'round'>,
 'patch.edgecolor': 'w',
 'patch.force_edgecolor': True,
 'image.cmap': 'rocket',
 'font.family': ['sans-serif'],
 'font.sans-serif': ['Arial',
  'DejaVu Sans',
  'Liberation Sans',
  'Bitstream Vera Sans',
  'sans-serif'],
 'xtick.bottom': False,
 'xtick.top': False,
 'ytick.left': False,
 'ytick.right': False,
 'axes.spines.left': True,
 'axes.spines.bottom': True,
 'axes.spines.right': True,
 'axes.spines.top': True}
 # 파라미터가 엄청 많다..

 

나는 아래와 같이 파라미터를 변경해주었다.

rc = {
    "axes.facecolor": "#F6F6F6",
    "figure.facecolor": "#B8B8B8",
    "axes.edgecolor": "#000000",
    "grid.color": "#C4C4C4",
    "axes.labelcolor": "#000000",
    "xtick.color": "#000000",
    "ytick.color": "#000000",
    "grid.alpha": 0.4
}

sns.set(rc=rc)

파라미터를 딕셔너리 형태로 저장한 뒤 sns.set(rc=rc) 코드로 쉽게 변경해줄 수 있다.

아래는 스타일에 따른 박스플롯 예시이다. 코드는 아래의 동일한 코드를 사용하였다.

# kaggle의 Spaceship Titanic 데이터셋 시각화 예시

fig, ax = plt.subplots(2,3,figsize=(15,8))
ax = ax.flatten()
fig.subplots_adjust(hspace=0.3, wspace=0.3)

for i, col in enumerate(train_num.columns):
  sns.boxplot(train, x="Transported", y=col, order=[0,1], palette='YlOrRd', ax=ax[i])
  ax[i].set_ylabel("")
  ax[i].set_xlabel("")
  ax[i].set_title(f"{col} vs Transported")


1. Default

seaborn의 default style


2. Customized

Customized plot

같은 데이터 시각화이지만 보기가 더 쉬운 것 같다. 앞으로 유용하게 적용할 것 같다.


참고자료

[1] "Controlling figure aesthetics", seaborn,
https://seaborn.pydata.org/tutorial/aesthetics.html

'Data Science > EDA Practice' 카테고리의 다른 글

[EDA Practice] Subplot 그리기  (0) 2023.02.09
[EDA Practice] Figure, Axes 객체  (0) 2023.02.09
[EDA Practice] 2023/W4 EDA Practice  (0) 2023.01.27
[EDA Practice] EDA란?  (0) 2023.01.20