Visualization

wandajeong·2022년 2월 12일
0

Data Handling

목록 보기
11/15

import

import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

plt.style.use(['dark_background'])
#sns.set_theme(style='whitegrid', font_scale=1)
#sns.set_palette('Set2', n_colors=10)

plt.rc('font', family='malgun gothic')   #style 지정 후에 지정해야함 
plt.rc('axes', unicode_minus=False)   #마이너스 방지 코드 

matplotlib

fig, (ax1, ax2, ax3) = plt.subplots(figsize=(24, 10), nrows=1, ncols=3)
fig.tight_layout()

Heatmap

corr_matrix = df.corr()
plt.figure(figsize=(15,12))
sns.heatmap(corr_matrix, annot=False, cmap='coolwarm', mask=np.triu(df.corr()) # 절반만 
plt.title("Correlation Heatmap", fontdict={'fontsize':16})
plt.show()

# correlation between target and features
(df.corr().loc['price']).plot(kind='barh', figsize=(4,10)))

# pandas로 심플하게 보기 
df.corr().style.background_gradient() 

여러개 바둑판 그리기

  • seaborn
# for문 활용 시계열 센서 데이터 분포 시각화
col_n = 3
row_n = 3

fig, ax = plt.subplots(ncols=col_n, nrows=row_n, figsize=(20,row_n*5))

for i,col in enumerate(df.columns[1:9]):
    sns.distplot(df_normal[col], ax=ax[int(i/col_n),int(i%col_n)])
    sns.distplot(df_anomaly[col], ax=ax[int(i/col_n),int(i%col_n)])
# ------------------------------------------------
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
plt.style.use(['dark_background'])

plt.figure(figsize = (20, 10))
plotnumber = 1

for column in numerical_list:
    if plotnumber <= 14:
        ax = plt.subplot(3, 5, plotnumber)
        sns.distplot(df[column])
        plt.xlabel(column)
        
    plotnumber += 1

plt.tight_layout()
plt.show()
  • plotly
import plotly.graph_objects as go
from plotly.subplots import make_subplots

col_n = 3
row_n = 10

fig = make_subplots(rows=row_n, cols=col_n, vertical_spacing=0.018, horizontal_spacing=0.05)

for i, (_, df) enumerate(df_dict.items()):
	fig.add_trace(go.Scatter(x=df['x'], y=df['y'], mode='lines', name='sum', line=dict(color='coral')), row=int((i+3)/col_n), col=int(i%col_n)+1)
    
fig.update_layout(height=3500, width=1800)
fig.update_xaxes(title_text='x_label')
fig.show()

plotly html

import os
import plotly
import plotly.express as px
import plotly.graph_objects as go 
from plotly.subplots import make_subplots

Fig = go.Figure()

for col in features.columns:
	fig.add_trace(go.Scatter(x=features.index, y=features[col], mode='lines', name=col, showlegend=True))
    
for template in ["plotly_white"]:
	fig.update_layout(template=template, showlegned=True)
    fig.show()
    
plotly.offline.plot(fig, filename='xxx.html')

기타

# sns plot 크기 조정이 안 될 경우
plt.rcParams['figure.figsize'] = [10, 10]
profile
ML/DL swimmer

0개의 댓글