(14주차) 12월8일
maplotlib theme
- 강의영상
- xkcd
- matplotlib
- 테마변경
- seaborn-dark로 예전강의노트 그리기
- seaborn-dark + xkcd 로 예전강의노트 그리기
- 추천포맷으로 예전강의노트 그리기
- 최신유행?
-
(1/3) matplotlib theme (1)
-
(2/3) matplotlib theme (2)
-
(3/3) matplotlib theme (3)
https://xkcd.com/386/)
Duty Calls (https://xkcd.com/353/)
Python (https://xkcd.com/1838/)
Machine Learning (-
많은 단점들이 느껴지겠지만 matplotlib은 전설적인 플랏임.
-
제생각: 파이썬에서 향후 어떠한 시각화 패키지가 나와도 matplotlib이 사장될 일은 없을 것 같다. $\to$ 일단 배워야 합니다.
-
제생각: 다 좋긴한데 그림이 너무 올드해보인다.
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()
-
아이쇼핑?
lst = plt.style.available
lst
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()
-
저는 이중에서 seaborn-dark를 선호했어요
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']
df
df.reset_index()
-
1개의 y를 그리기
with plt.style.context('seaborn-dark'):
df.reset_index().plot.line(x='Date',y='AMZN')
-
2개의 y를 겹쳐그리기
with plt.style.context('seaborn-dark'):
df.reset_index().plot.line(x='Date',y=['AMZN','GOOG'])
-
모든 y겹처그리기
with plt.style.context('seaborn-dark'):
df.reset_index().plot.line(x='Date')
-
그림크기 조정
with plt.style.context('seaborn-dark'):
df.reset_index().plot.line(x='Date',figsize=(10,10))
-
서브플랏
with plt.style.context('seaborn-dark'):
df.reset_index().plot.line(x='Date',figsize=(10,10),subplots=True)
-
레이아웃 조정
with plt.style.context('seaborn-dark'):
df.reset_index().plot.line(x='Date',figsize=(10,10),subplots=True,layout=(4,2))
-
투명도 조정
with plt.style.context('seaborn-dark'):
df.reset_index().plot.line(x='Date',figsize=(10,10),subplots=True,layout=(4,2),alpha=0.3)
-
레전드 삭제
with plt.style.context('seaborn-dark'):
df.reset_index().plot.line(x='Date',figsize=(10,10),subplots=True,layout=(4,2),legend=False)
df = pd.read_csv('https://raw.githubusercontent.com/kalilurrahman/datasets/main/mobilephonemktshare2020.csv')
df
with plt.style.context('seaborn-dark'):
df.plot.bar(x='Date',y=['Samsung','Apple'],figsize=(10,5))
with plt.style.context('seaborn-dark'):
df.plot.bar(x='Date',y=['Samsung','Apple'],figsize=(10,5),width=0.8)
with plt.style.context('seaborn-dark'):
df.plot.barh(x='Date',y=['Samsung','Apple'],figsize=(5,10))
with plt.style.context('seaborn-dark'):
df.plot.bar(x='Date',figsize=(15,10),subplots=True,layout=(4,4),legend=False)
- 이건 사실 라인플랏으로 그려도 괜찮음
-
비율을 평균내는 것은 이상하지만 시각화예제를 위해서 제조사별로 평균점유율을 시각화하여보자.
import numpy as np
with plt.style.context('seaborn-dark'):
df.melt(id_vars='Date').groupby('variable').agg(np.mean).\
plot.bar(legend=False)
-
소팅을 한뒤에 시각화해보자.
with plt.style.context('seaborn-dark'):
df.melt(id_vars='Date').groupby('variable').agg(np.mean).sort_values('value',ascending=False).\
plot.bar(legend=False)
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.bar(x='Date',y='value',color='variable',backend='plotly',width=500,height=600)
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')
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)
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',facet_col='variable')
show(fig)
fig=df.melt(id_vars='Date').query("variable=='Samsung' or variable=='Apple' or variable=='Huawei'" ).\
plot.bar(y='Date',x='value',color='variable',backend='plotly',facet_row='variable',height=700)
show(fig)
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']
df
df.reset_index()
-
1개의 y를 그리기
with plt.style.context('seaborn-dark'):
matplotlib.pyplot.xkcd(scale=1,length=200)
df.reset_index().plot.line(x='Date',y='AMZN')
-
2개의 y를 겹쳐그리기
with plt.style.context('seaborn-dark'):
matplotlib.pyplot.xkcd(scale=1,length=200)
df.reset_index().plot.line(x='Date',y=['AMZN','GOOG'])
-
모든 y겹처그리기
with plt.style.context('seaborn-dark'):
matplotlib.pyplot.xkcd(scale=1,length=200)
df.reset_index().plot.line(x='Date')
-
그림크기 조정
with plt.style.context('seaborn-dark'):
matplotlib.pyplot.xkcd(scale=1,length=200)
df.reset_index().plot.line(x='Date',figsize=(10,10))
-
서브플랏
with plt.style.context('seaborn-dark'):
matplotlib.pyplot.xkcd(scale=1,length=200)
df.reset_index().plot.line(x='Date',figsize=(10,10),subplots=True)
-
레이아웃 조정
with plt.style.context('seaborn-dark'):
matplotlib.pyplot.xkcd(scale=1,length=200)
df.reset_index().plot.line(x='Date',figsize=(10,10),subplots=True,layout=(4,2))
-
투명도 조정
with plt.style.context('seaborn-dark'):
matplotlib.pyplot.xkcd(scale=1,length=200)
df.reset_index().plot.line(x='Date',figsize=(10,10),subplots=True,layout=(4,2),alpha=0.3)
-
레전드 삭제
with plt.style.context('seaborn-dark'):
matplotlib.pyplot.xkcd(scale=1,length=200)
df.reset_index().plot.line(x='Date',figsize=(10,10),subplots=True,layout=(4,2),legend=False)
df = pd.read_csv('https://raw.githubusercontent.com/kalilurrahman/datasets/main/mobilephonemktshare2020.csv')
df
with plt.style.context('seaborn-dark'):
matplotlib.pyplot.xkcd(scale=1,length=200)
df.plot.bar(x='Date',y=['Samsung','Apple'],figsize=(10,5))
with plt.style.context('seaborn-dark'):
matplotlib.pyplot.xkcd(scale=1,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=1,length=200)
df.plot.barh(x='Date',y=['Samsung','Apple'],figsize=(5,10))
with plt.style.context('seaborn-dark'):
matplotlib.pyplot.xkcd(scale=1,length=200)
df.plot.bar(x='Date',figsize=(15,10),subplots=True,layout=(4,4),legend=False)
- 이건 사실 라인플랏으로 그려도 괜찮음
-
비율을 평균내는 것은 이상하지만 시각화예제를 위해서 제조사별로 평균점유율을 시각화하여보자.
with plt.style.context('seaborn-dark'):
matplotlib.pyplot.xkcd(scale=1,length=200)
df.melt(id_vars='Date').groupby('variable').agg(np.mean).\
plot.bar(legend=False)
-
소팅을 한뒤에 시각화해보자.
with plt.style.context('seaborn-dark'):
matplotlib.pyplot.xkcd(scale=1,length=200)
df.melt(id_vars='Date').groupby('variable').agg(np.mean).sort_values('value',ascending=False).\
plot.bar(legend=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']
df
df.reset_index()
-
1개의 y를 그리기
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','GOOG'])
-
모든 y겹처그리기
with plt.style.context('seaborn-dark'):
matplotlib.pyplot.xkcd(scale=0,length=200)
df.reset_index().plot.line(x='Date')
-
그림크기 조정
with plt.style.context('seaborn-dark'):
matplotlib.pyplot.xkcd(scale=0,length=200)
df.reset_index().plot.line(x='Date',figsize=(10,10))
-
서브플랏
with plt.style.context('seaborn-dark'):
matplotlib.pyplot.xkcd(scale=0,length=200)
df.reset_index().plot.line(x='Date',figsize=(10,10),subplots=True)
-
레이아웃 조정
with plt.style.context('seaborn-dark'):
matplotlib.pyplot.xkcd(scale=0,length=200)
df.reset_index().plot.line(x='Date',figsize=(10,10),subplots=True,layout=(4,2))
-
투명도 조정
with plt.style.context('seaborn-dark'):
matplotlib.pyplot.xkcd(scale=0,length=200)
df.reset_index().plot.line(x='Date',figsize=(10,10),subplots=True,layout=(4,2),alpha=0.3)
-
레전드 삭제
with plt.style.context('seaborn-dark'):
matplotlib.pyplot.xkcd(scale=0,length=200)
df.reset_index().plot.line(x='Date',figsize=(10,10),subplots=True,layout=(4,2),legend=False)
df = pd.read_csv('https://raw.githubusercontent.com/kalilurrahman/datasets/main/mobilephonemktshare2020.csv')
df
with plt.style.context('seaborn-dark'):
matplotlib.pyplot.xkcd(scale=0,length=200)
df.plot.bar(x='Date',y=['Samsung','Apple'],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=['Samsung','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)
- 이건 사실 라인플랏으로 그려도 괜찮음
-
비율을 평균내는 것은 이상하지만 시각화예제를 위해서 제조사별로 평균점유율을 시각화하여보자.
with plt.style.context('seaborn-dark'):
matplotlib.pyplot.xkcd(scale=0,length=200)
df.melt(id_vars='Date').groupby('variable').agg(np.mean).\
plot.bar(legend=False)
-
소팅을 한뒤에 시각화해보자.
with plt.style.context('seaborn-dark'):
matplotlib.pyplot.xkcd(scale=0,length=200)
df.melt(id_vars='Date').groupby('variable').agg(np.mean).sort_values('value',ascending=False).\
plot.bar(legend=False)
-
mplcyberpunk의 소개 및 사용법
import 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']
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()
-
서브플랏에는 add_glow_effects()
적용안됨
with plt.style.context('cyberpunk'):
#matplotlib.pyplot.xkcd(scale=0,length=200)
df.reset_index().plot.line(x='Date',figsize=(10,10),subplots=True,layout=(4,2))
mplcyberpunk.add_glow_effects()