jupyter notebook or colab 환경에서 사용할수있는 동적 시각화 패키지(?) 사용

 

인터랙션 시각화 1편은 plotly를 사용한 인터랙션 시각화로 구성되어있습니다

 

 

1. 선형회귀 + 산포도

import plotly.express as px

df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", trendline="ols")
fig.show()

express를 사용한 데이터, 결제금과 팁 간의 선형회귀 + 산점도(산포도)

기본적이지만 데이터의 구조를 파악하는데 도움이 많이 되는 시각화 방법입니다

 

 

 

2. gapminder data 분포도

import plotly.express as px

df = px.data.gapminder().query("year == 2007").query("continent == 'Europe'")
df.loc[df['pop'] < 2.e6, 'country'] = 'Other countries' # Represent only large countries
fig = px.pie(df, values='pop', names='country', title='Population of European continent')
fig.show()

유럽 인구 분포도

 

 

 

 

3. pie 분포

import plotly.express as px

df = px.data.tips()
fig = px.pie(df, values='tip', names='day', color_discrete_sequence=px.colors.sequential.RdBu)
fig.show()

 

 

 

 

 

4. go pie 분포 pull

import plotly.graph_objects as go

labels = ['a','b','c','d']
values = [4500, 2500, 1053, 500]

fig = go.Figure(data=[go.Pie(labels=labels, values=values, pull=[0, 0, 0.2, 0])])
fig.show()

 

 

 

 

 

5. distplot

import plotly.figure_factory as ff
import numpy as np

# Add histogram data
x1 = np.random.randn(200) - 2
x2 = np.random.randn(200)
x3 = np.random.randn(200) + 2
x4 = np.random.randn(200) + 4

# Group data together
hist_data = [x1, x2, x3, x4]

group_labels = ['Group 1', 'Group 2', 'Group 3', 'Group 4']

# Create distplot with custom bin_size
fig = ff.create_distplot(hist_data, group_labels, bin_size=.2)
fig.show()

 

 

 

6. Box plot

import plotly.express as px

df = px.data.tips()

fig = px.box(df, x="day", y="total_bill", color="smoker")
fig.show()

개인적으로 박스 플롯이 데이터 분석에 도움이 많이 되는 시각화 그래프라고 생각해요 예외 데이터 확인과 핵심데이터 구간 확인이 동시에 되는 효율적인 시각화 입니다 *_*

 

 

 

 

 

7.  histgram

import plotly.express as px
df = px.data.tips()

fig = px.density_heatmap(df, x="total_bill", y="tip", marginal_x="histogram", marginal_y="histogram")
fig.show()

데이터 분석 관계 분석, 결론 도출을 위한 시각화 방법으로 matplolib, seaborn을 사용해보겠습니다

 

 

1. 패키지 설치

pip install matplotlib
pip install seaborn

 

 

 

 

 

2. 기본 사용법

from matplotlib import pyplot as plt

x = np.arange( 0, 12, 0.01 )
y = np.sin(x)

plt.figure( figsize=(10, 6) )

plt.plot( x, y,  label='sin wave')
plt.plot( x, np.cos(x), label='cos wave' )

numpy의 np.sin을 통해 0~12의 sin값을 예시 그래프 값으로 사용한 차트입니다

 

 

 

3. seaborn boxplot

import seaborn as sns

plt.figure( figsize=(10,6) )
sns.boxplot( x )
plt.show()

파란 박스 구간을 통해 데이터의 중간값들은 17 주변으로 확인되고 대부분 14 ~ 22 사이에 있음을 알 수있다

최대치는 52정도이고 40 이상의 데이터는 많이 없음을 알수있다

 

데이터 결론 도출로는 14 ~ 22의 데이터가 많이 사용되므로 중점적으로 해당 데이터는 가격대로 해당 층의 니즈를 공략해야한다는 결론이 나올수있다

 

 

 

4. seaborn swarmplot

plt.figure( figsize=(10,6) )
sns.boxplot( x='day', y='total_bill', data=tips, palette='Set3' ) 
sns.swarmplot( x='day', y='total_bill', data=tips, color='0.5')
plt.show()

day는 요일로 목금토일을 설정했으며 데이터를 통해 금요일에는 손님이 많이 없으며 주말에 조금 더 높은 가격대를 지출함을 알 수 있다

 

 

 

 

5. seaborn Implot

plt.figure( figsize=(10,5) )
sns.Implot( x='total_bill', y='tip', data=tips, size=10 )
plt.show()

Implot은 컬럼과의 관계 분석을 시각화한 차트로 total_bill 매출과 tip 팁의 관계를 도출한다

 

x로 설정한 매출과 산포도로 측정된 tip과의 관계차트이다

 

x, y 데이터가 얼마나 떨어져 있나와 파란색 영역을 통해 표준편차를 볼 수 있습니다

 

 

 

 

6. seaborn heatmap

plt.figure( figsize=(10,10) )
sns.heatmap(df, annot=True, fmt='d'  )
plt.show()

표는 x year, y month로 연도별 월 고객수를 시각화 한것으로 색이 밝아질수록 고객이 많음을 뜻합니다

 

차트에서는 연도가 지나갈수록 고객이 많아지고 여름에 가장 고객이 많음으로 여름장사에 집중됨을 알 수 있습니다

 

 

 

7. seaborn pairplot

sns.pairplot( iris, hue='species', x_vars=['sepal_width', 'sepal_length'],  y_vars=['petal_width', 'petal_length'])
plt.show()

산포 행렬 시각화 방법으로 데이터의 군집을 확인할 수 있습니다

 

군집데이터의 사용 능력치는 딥러닝에서 빛을 바라기에 활용 방법을 많이 익혀두는게 좋습니다 저 또한 군집데이터에 대한 이해도를 꾸준히 높이고 있습니다

 

 

 

 

8. seaborn 다중 histogram

for spec in iris.species.unique():  
  spec_df = iris[ iris.species == spec ]
  print( spec, spec_df.shape )
  spec_df[ 'petal_width' ].plot( kind='hist', alpha=0.4, label=spec)

plt.legend()
plt.show()

데이터의 분포를 확인하기 좋은 시각화 방법으로 Implot과 heatmap, histogram 시각화를 가장 많이 사용하게 되는것 같습니다

+ Recent posts