데이터시각화 특강 (15주차) 12월 20일
ckcd
Duty calls(https://xkcd.com/386/)
Duty calls(https://xkcd.com/353/)
Duty calls(https://xkcd.com/31838/)
-
단점이 많이 존재, 전설적인(?) plot
-
더 보기 편리한 시각화 해보기
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
-
기본
plt.plot([1,2,3,4],[2,3,4,3],'o--')
-
다른 테마
plt.style.available
- 다양한 스타일 존재
with plt.style.context('Solarize_Light2'):
plt.plot([1,2,3,4],[2,3,4,3],'o--')
plt.show()
- with 쓰면 테마 여기서는 바뀌지만 기본은 바뀌지 않는 것 같음
lst = plt.style.available
for l in lst:
with plt.style.context(l):
plt.plot([1,2,3,4],[2,3,4,3],'o--')
plt.title(l)
plt.show()
- 다양한 테마 보기
import numpy as np
import pandas as pd
import warnings
from IPython.display import HTML
from pandas_datareader import data as pdr
def show(fig):
return HTML(fig.to_html(include_plotlyjs='cdn',include_mathjax=False, config=dict({'scrollZoom':False})))
symbols = ['AMZN','AAPL','GOOG','MSFT','NFLX','NVDA','TSLA']
start = '2020-01-01'
end = '2020-11-28'
df = pdr.get_data_yahoo(symbols,start,end)['Adj Close']
-
1개의 y를 그리기
matplotlib.pyplot.xkcd(scale=1,length=200)
- scale 조정에 따라 축의 ~이 달라지는 듯..!
- 0이면 거의 직선이 돼
with plt.style.context('seaborn-dark'):
matplotlib.pyplot.xkcd(scale=0,length=200)
df.reset_index().plot.line(x='Date',y='AMZN')
-
2개의 y를 겹쳐그리기
with plt.style.context('seaborn-dark'):
matplotlib.pyplot.xkcd(scale=0,length=200)
df.reset_index().plot.line(x='Date',y=['AMZN','TSLA'])
-
모든 y겹처그리기, 및 그림 크기 조정
with plt.style.context('seaborn-dark'):
matplotlib.pyplot.xkcd(scale=0,length=200)
df.reset_index().plot.line(x='Date',figsize=(8,8))
-
서브플랏, 레이아웃 조정, 폰트조정, 투명도 조정, 레전드 삭제
with plt.style.context('seaborn-dark'):
matplotlib.pyplot.xkcd(scale=0,length=200)
df.reset_index().plot.line(x='Date',figsize=(10,8),subplots=True,layout=(4,2),fontsize=6,alpha=0.3, legend=False)
df = pd.read_csv('https://raw.githubusercontent.com/kalilurrahman/datasets/main/mobilephonemktshare2020.csv')
with plt.style.context('seaborn-dark'):
matplotlib.pyplot.xkcd(scale=0,length=200)
df.plot.bar(x='Date',y=['Samsung','Huawei'],figsize=(10,5))
with plt.style.context('seaborn-dark'):
matplotlib.pyplot.xkcd(scale=0,length=200)
df.plot.bar(x='Date',y=['Samsung','Apple'],figsize=(10,5),width=0.8)
with plt.style.context('seaborn-dark'):
matplotlib.pyplot.xkcd(scale=0,length=200)
df.plot.barh(x='Date',y=['Huawei','Apple'],figsize=(5,10))
with plt.style.context('seaborn-dark'):
matplotlib.pyplot.xkcd(scale=0,length=200)
df.plot.bar(x='Date',figsize=(15,10),subplots=True,layout=(4,4),legend=False)
-
비율을 평균내는 것은 이상하지만 시각화예제를 위해서 제조사별로 평균점유율을 sorting 한 후 시각화하여보자.
with plt.style.context('seaborn-dark'):
matplotlib.pyplot.xkcd(scale=0,length=100)
df.melt(id_vars='Date').groupby('variable').agg(np.mean).sort_values('value',ascending=False).\
plot.bar(legend=False)
wide form 이 아닌 long form, 즉, tidy data일때 사용
fig= df.melt(id_vars='Date').groupby('variable').agg(np.mean).sort_values('value',ascending=False).\
plot.bar(backend='plotly')
show(fig)
fig=df.melt(id_vars='Date').\
plot.barh(y='Date',x='value',color='variable',backend='plotly',width=800,height=500)
show(fig)
fig=df.melt(id_vars='Date').query("variable=='Samsung' or variable=='Apple' or variable=='Huawei'" ).\
plot.bar(x='Date',y='value',color='variable',backend='plotly',barmode='group',text='value')
show(fig)
-
mplcyberpunk 소개 및 사용법
symbols = ['AMZN','AAPL','GOOG','MSFT','NFLX','NVDA','TSLA']
start = '2020-01-01'
end = '2020-11-28'
df = pdr.get_data_yahoo(symbols,start,end)['Adj Close']
import mplcyberpunk
with plt.style.context('cyberpunk'):
#matplotlib.pyplot.xkcd(scale=0,length=200)
df.reset_index().plot.line(x='Date',y=['AMZN','GOOG'])
mplcyberpunk.add_glow_effects() # 선 밝게
mplcyberpunk.add_underglow() # 선 아래 번짐없이 뚜렷하게
-
subplot에는 mplcyberpunk.add_glow_effects() 적용이 안 된다..
with plt.style.context('cyberpunk'):
df.reset_index().plot.line(x='Date',figsize=(10,8),subplots=True,layout=(4,2),fontsize=6,alpha=0.3, legend=False)
mplcyberpunk.add_glow_effects()
mplcyberpunk.add_underglow()