Seaborn
- Python의 visualization 모듈
- Matplotlib 보다 디자인이 더 깔끔하고 다양하다.
sns.countplot(x="size", data=df)
사용 예시
# figsize 조정
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(18, 3))
ax = sns.barplot(data=df, x="date", y="수익률(%)", ax=ax);
# x tick label 45도 돌리기
current_x_tick_label = ax.get_xticklabels()
ax.set_xticklabels(current_x_tick_label, rotation=45);
- matplotlib : tidy form data(raw data)에 대해 groupby + aggregation 등의 operation 별도로 진행 후에 visualization 진행
- seaborn : 별도의 operation 없이 plot을 진행하면, 알아서 aggegation 까지 진행하여 visualization
hue 추가
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(18, 3))
sns.barplot(data=df, x="date", y="수익률(%)", ax=ax, hue="size")
current_x_tick_label = ax.get_xticklabels()
ax.set_xticklabels(current_x_tick_label, rotation=45);
relation plot (다차원 그래프)
sns.relplot(
x="PBR(IFRS-연결)",
y="수익률(%)",
col="size",
hue="베타 (M,5Yr)",
data=df,
palette="coolwarm",
)
* 실전 예제
# Get Data
df_list = []
for i in range(2015, 2018):
df_list.append(
pd.read_csv("my_data/naver_finance/{}_12.csv".format(i))
)
df = pd.concat(df_list) # 데이터 연결
df = df.dropna() # NAN 제거
df['rtn'] = df['price2'] / df['price'] - 1 # col 추가
# outlier(이상치) 제거하기
for col in df.columns:
if col not in ['ticker', 'price2', 'price', 'rtn']:
mu = df[col].mean()
std = df[col].std()
cond1 = mu - 2*std <= df[col]
cond2 = df[col] <= mu + 2*std
df = df[cond1 & cond2]
순이익률 - RTN 상관관계 파악
sns.relplot(
x="순이익률(%)",
y="rtn",
hue="ROA(%)",
palette="coolwarm",
data=df
)
* Seaborn plot 종류 : https://seaborn.pydata.org/examples/index.html
Example gallery — seaborn 0.11.2 documentation
seaborn.pydata.org
'Quant' 카테고리의 다른 글
Parameter Optimization (0) | 2025.02.03 |
---|---|
Quant : Backtesting - 재무제표 기반 (0) | 2022.04.27 |
Quant : Visualization - Matplotlib (0) | 2022.04.13 |
Quant : Pandas - 데이터 합치기 (0) | 2022.04.05 |
Quant : Pandas - Grouping (0) | 2022.03.27 |