강의영상

- (1/3) matplotlib theme (1)

- (2/3) matplotlib theme (2)

- (3/3) matplotlib theme (3)

xkcd

Machine Learning (https://xkcd.com/1838/)

matplotlib

- 많은 단점들이 느껴지겠지만 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--')
[<matplotlib.lines.Line2D at 0x7f3cc3cc2970>]

- 다른테마

plt.style.available
['Solarize_Light2',
 '_classic_test_patch',
 'bmh',
 'classic',
 'dark_background',
 'fast',
 'fivethirtyeight',
 'ggplot',
 'grayscale',
 'seaborn',
 'seaborn-bright',
 'seaborn-colorblind',
 'seaborn-dark',
 'seaborn-dark-palette',
 'seaborn-darkgrid',
 'seaborn-deep',
 'seaborn-muted',
 'seaborn-notebook',
 'seaborn-paper',
 'seaborn-pastel',
 'seaborn-poster',
 'seaborn-talk',
 'seaborn-ticks',
 'seaborn-white',
 'seaborn-whitegrid',
 'tableau-colorblind10']
with plt.style.context('Solarize_Light2'): 
    plt.plot([1,2,3,4],[2,3,4,3],'o--')
plt.show()

- 아이쇼핑?

lst = plt.style.available
lst
['Solarize_Light2',
 '_classic_test_patch',
 'bmh',
 'classic',
 'dark_background',
 'fast',
 'fivethirtyeight',
 'ggplot',
 'grayscale',
 'seaborn',
 'seaborn-bright',
 'seaborn-colorblind',
 'seaborn-dark',
 'seaborn-dark-palette',
 'seaborn-darkgrid',
 'seaborn-deep',
 'seaborn-muted',
 'seaborn-notebook',
 'seaborn-paper',
 'seaborn-pastel',
 'seaborn-poster',
 'seaborn-talk',
 'seaborn-ticks',
 'seaborn-white',
 'seaborn-whitegrid',
 'tableau-colorblind10']
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를 선호했어요

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})))

line

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
Symbols AMZN AAPL GOOG MSFT NFLX NVDA TSLA
Date
2019-12-31 1847.839966 72.337982 1337.020020 154.749741 323.570007 58.676846 83.666000
2020-01-02 1898.010010 73.988472 1367.369995 157.615097 329.809998 59.826439 86.052002
2020-01-03 1874.969971 73.269150 1360.660034 155.652512 325.899994 58.868862 88.601997
2020-01-06 1902.880005 73.852982 1394.209961 156.054855 335.829987 59.115738 90.307999
2020-01-07 1906.859985 73.505646 1393.339966 154.631958 330.750000 59.831425 93.811996
... ... ... ... ... ... ... ...
2020-11-20 3099.399902 116.621048 1742.189941 208.641098 488.239990 130.724655 489.609985
2020-11-23 3098.389893 113.152443 1734.859985 208.363434 476.619995 131.246567 521.849976
2020-11-24 3118.060059 114.464355 1768.880005 212.082260 482.880005 129.426178 555.380005
2020-11-25 3185.070068 115.319077 1771.430054 212.092178 485.000000 132.192947 574.000000
2020-11-27 3195.340088 115.875641 1793.189941 213.440872 491.359985 132.457642 585.760010

231 rows × 7 columns

df.reset_index()
Symbols Date AMZN AAPL GOOG MSFT NFLX NVDA TSLA
0 2019-12-31 1847.839966 72.337982 1337.020020 154.749741 323.570007 58.676846 83.666000
1 2020-01-02 1898.010010 73.988472 1367.369995 157.615097 329.809998 59.826439 86.052002
2 2020-01-03 1874.969971 73.269150 1360.660034 155.652512 325.899994 58.868862 88.601997
3 2020-01-06 1902.880005 73.852982 1394.209961 156.054855 335.829987 59.115738 90.307999
4 2020-01-07 1906.859985 73.505646 1393.339966 154.631958 330.750000 59.831425 93.811996
... ... ... ... ... ... ... ... ...
226 2020-11-20 3099.399902 116.621048 1742.189941 208.641098 488.239990 130.724655 489.609985
227 2020-11-23 3098.389893 113.152443 1734.859985 208.363434 476.619995 131.246567 521.849976
228 2020-11-24 3118.060059 114.464355 1768.880005 212.082260 482.880005 129.426178 555.380005
229 2020-11-25 3185.070068 115.319077 1771.430054 212.092178 485.000000 132.192947 574.000000
230 2020-11-27 3195.340088 115.875641 1793.189941 213.440872 491.359985 132.457642 585.760010

231 rows × 8 columns

- 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)

bar, barh

df = pd.read_csv('https://raw.githubusercontent.com/kalilurrahman/datasets/main/mobilephonemktshare2020.csv')
df
Date Samsung Apple Huawei Xiaomi Oppo Mobicel Motorola LG Others Realme Google Nokia Lenovo OnePlus Sony Asus
0 2019-10 31.49 22.09 10.02 7.79 4.10 3.15 2.41 2.40 9.51 0.54 2.35 0.95 0.96 0.70 0.84 0.74
1 2019-11 31.36 22.90 10.18 8.16 4.42 3.41 2.40 2.40 9.10 0.78 0.66 0.97 0.97 0.73 0.83 0.75
2 2019-12 31.37 24.79 9.95 7.73 4.23 3.19 2.50 2.54 8.13 0.84 0.75 0.90 0.87 0.74 0.77 0.70
3 2020-01 31.29 24.76 10.61 8.10 4.25 3.02 2.42 2.40 7.55 0.88 0.69 0.88 0.86 0.79 0.80 0.69
4 2020-02 30.91 25.89 10.98 7.80 4.31 2.89 2.36 2.34 7.06 0.89 0.70 0.81 0.77 0.78 0.80 0.69
5 2020-03 30.80 27.03 10.70 7.70 4.30 2.87 2.35 2.28 6.63 0.93 0.73 0.72 0.74 0.78 0.76 0.66
6 2020-04 30.41 28.79 10.28 7.60 4.20 2.75 2.51 2.28 5.84 0.90 0.75 0.69 0.71 0.80 0.76 0.70
7 2020-05 30.18 26.72 10.39 8.36 4.70 3.12 2.46 2.19 6.31 1.04 0.70 0.73 0.77 0.81 0.78 0.76
8 2020-06 31.06 25.26 10.69 8.55 4.65 3.18 2.57 2.11 6.39 1.04 0.68 0.74 0.75 0.77 0.78 0.75
9 2020-07 30.95 24.82 10.75 8.94 4.69 3.46 2.45 2.03 6.41 1.13 0.65 0.76 0.74 0.76 0.75 0.72
10 2020-08 31.04 25.15 10.73 8.90 4.69 3.38 2.39 1.96 6.31 1.18 0.63 0.74 0.72 0.75 0.73 0.70
11 2020-09 30.57 24.98 10.58 9.49 4.94 3.50 2.27 1.88 6.12 1.45 0.63 0.74 0.67 0.81 0.69 0.67
12 2020-10 30.25 26.53 10.44 9.67 4.83 2.54 2.21 1.79 6.04 1.55 0.63 0.69 0.65 0.85 0.67 0.64
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)

bar, barh (plotly와 비교)

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)

seaborn-dark + xkcd 로 예전강의노트 그리기

line

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
Symbols AMZN AAPL GOOG MSFT NFLX NVDA TSLA
Date
2019-12-31 1847.839966 72.337990 1337.020020 154.749756 323.570007 58.676846 83.666000
2020-01-02 1898.010010 73.988472 1367.369995 157.615112 329.809998 59.826443 86.052002
2020-01-03 1874.969971 73.269157 1360.660034 155.652496 325.899994 58.868858 88.601997
2020-01-06 1902.880005 73.852982 1394.209961 156.054871 335.829987 59.115734 90.307999
2020-01-07 1906.859985 73.505638 1393.339966 154.632004 330.750000 59.831432 93.811996
... ... ... ... ... ... ... ...
2020-11-20 3099.399902 116.621048 1742.189941 208.641129 488.239990 130.724670 489.609985
2020-11-23 3098.389893 113.152443 1734.859985 208.363449 476.619995 131.246567 521.849976
2020-11-24 3118.060059 114.464355 1768.880005 212.082260 482.880005 129.426178 555.380005
2020-11-25 3185.070068 115.319077 1771.430054 212.092178 485.000000 132.192963 574.000000
2020-11-27 3195.340088 115.875641 1793.189941 213.440872 491.359985 132.457657 585.760010

231 rows × 7 columns

df.reset_index()
Symbols Date AMZN AAPL GOOG MSFT NFLX NVDA TSLA
0 2019-12-31 1847.839966 72.337990 1337.020020 154.749756 323.570007 58.676846 83.666000
1 2020-01-02 1898.010010 73.988472 1367.369995 157.615112 329.809998 59.826443 86.052002
2 2020-01-03 1874.969971 73.269157 1360.660034 155.652496 325.899994 58.868858 88.601997
3 2020-01-06 1902.880005 73.852982 1394.209961 156.054871 335.829987 59.115734 90.307999
4 2020-01-07 1906.859985 73.505638 1393.339966 154.632004 330.750000 59.831432 93.811996
... ... ... ... ... ... ... ... ...
226 2020-11-20 3099.399902 116.621048 1742.189941 208.641129 488.239990 130.724670 489.609985
227 2020-11-23 3098.389893 113.152443 1734.859985 208.363449 476.619995 131.246567 521.849976
228 2020-11-24 3118.060059 114.464355 1768.880005 212.082260 482.880005 129.426178 555.380005
229 2020-11-25 3185.070068 115.319077 1771.430054 212.092178 485.000000 132.192963 574.000000
230 2020-11-27 3195.340088 115.875641 1793.189941 213.440872 491.359985 132.457657 585.760010

231 rows × 8 columns

- 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)

bar, barh

df = pd.read_csv('https://raw.githubusercontent.com/kalilurrahman/datasets/main/mobilephonemktshare2020.csv')
df
Date Samsung Apple Huawei Xiaomi Oppo Mobicel Motorola LG Others Realme Google Nokia Lenovo OnePlus Sony Asus
0 2019-10 31.49 22.09 10.02 7.79 4.10 3.15 2.41 2.40 9.51 0.54 2.35 0.95 0.96 0.70 0.84 0.74
1 2019-11 31.36 22.90 10.18 8.16 4.42 3.41 2.40 2.40 9.10 0.78 0.66 0.97 0.97 0.73 0.83 0.75
2 2019-12 31.37 24.79 9.95 7.73 4.23 3.19 2.50 2.54 8.13 0.84 0.75 0.90 0.87 0.74 0.77 0.70
3 2020-01 31.29 24.76 10.61 8.10 4.25 3.02 2.42 2.40 7.55 0.88 0.69 0.88 0.86 0.79 0.80 0.69
4 2020-02 30.91 25.89 10.98 7.80 4.31 2.89 2.36 2.34 7.06 0.89 0.70 0.81 0.77 0.78 0.80 0.69
5 2020-03 30.80 27.03 10.70 7.70 4.30 2.87 2.35 2.28 6.63 0.93 0.73 0.72 0.74 0.78 0.76 0.66
6 2020-04 30.41 28.79 10.28 7.60 4.20 2.75 2.51 2.28 5.84 0.90 0.75 0.69 0.71 0.80 0.76 0.70
7 2020-05 30.18 26.72 10.39 8.36 4.70 3.12 2.46 2.19 6.31 1.04 0.70 0.73 0.77 0.81 0.78 0.76
8 2020-06 31.06 25.26 10.69 8.55 4.65 3.18 2.57 2.11 6.39 1.04 0.68 0.74 0.75 0.77 0.78 0.75
9 2020-07 30.95 24.82 10.75 8.94 4.69 3.46 2.45 2.03 6.41 1.13 0.65 0.76 0.74 0.76 0.75 0.72
10 2020-08 31.04 25.15 10.73 8.90 4.69 3.38 2.39 1.96 6.31 1.18 0.63 0.74 0.72 0.75 0.73 0.70
11 2020-09 30.57 24.98 10.58 9.49 4.94 3.50 2.27 1.88 6.12 1.45 0.63 0.74 0.67 0.81 0.69 0.67
12 2020-10 30.25 26.53 10.44 9.67 4.83 2.54 2.21 1.79 6.04 1.55 0.63 0.69 0.65 0.85 0.67 0.64
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)
findfont: Font family ['xkcd', 'xkcd Script', 'Humor Sans', 'Comic Neue', 'Comic Sans MS'] not found. Falling back to DejaVu Sans.
  • 이건 사실 라인플랏으로 그려도 괜찮음

- 비율을 평균내는 것은 이상하지만 시각화예제를 위해서 제조사별로 평균점유율을 시각화하여보자.

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)

추천포맷으로 예전강의노트 그리기

line

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
Symbols AMZN AAPL GOOG MSFT NFLX NVDA TSLA
Date
2019-12-31 1847.839966 72.337990 1337.020020 154.749756 323.570007 58.676849 83.666000
2020-01-02 1898.010010 73.988480 1367.369995 157.615097 329.809998 59.826443 86.052002
2020-01-03 1874.969971 73.269165 1360.660034 155.652527 325.899994 58.868862 88.601997
2020-01-06 1902.880005 73.852982 1394.209961 156.054855 335.829987 59.115738 90.307999
2020-01-07 1906.859985 73.505646 1393.339966 154.631989 330.750000 59.831425 93.811996
... ... ... ... ... ... ... ...
2020-11-20 3099.399902 116.621048 1742.189941 208.641129 488.239990 130.724670 489.609985
2020-11-23 3098.389893 113.152443 1734.859985 208.363434 476.619995 131.246567 521.849976
2020-11-24 3118.060059 114.464355 1768.880005 212.082260 482.880005 129.426178 555.380005
2020-11-25 3185.070068 115.319077 1771.430054 212.092178 485.000000 132.192963 574.000000
2020-11-27 3195.340088 115.875641 1793.189941 213.440872 491.359985 132.457657 585.760010

231 rows × 7 columns

df.reset_index()
Symbols Date AMZN AAPL GOOG MSFT NFLX NVDA TSLA
0 2019-12-31 1847.839966 72.337990 1337.020020 154.749756 323.570007 58.676849 83.666000
1 2020-01-02 1898.010010 73.988480 1367.369995 157.615097 329.809998 59.826443 86.052002
2 2020-01-03 1874.969971 73.269165 1360.660034 155.652527 325.899994 58.868862 88.601997
3 2020-01-06 1902.880005 73.852982 1394.209961 156.054855 335.829987 59.115738 90.307999
4 2020-01-07 1906.859985 73.505646 1393.339966 154.631989 330.750000 59.831425 93.811996
... ... ... ... ... ... ... ... ...
226 2020-11-20 3099.399902 116.621048 1742.189941 208.641129 488.239990 130.724670 489.609985
227 2020-11-23 3098.389893 113.152443 1734.859985 208.363434 476.619995 131.246567 521.849976
228 2020-11-24 3118.060059 114.464355 1768.880005 212.082260 482.880005 129.426178 555.380005
229 2020-11-25 3185.070068 115.319077 1771.430054 212.092178 485.000000 132.192963 574.000000
230 2020-11-27 3195.340088 115.875641 1793.189941 213.440872 491.359985 132.457657 585.760010

231 rows × 8 columns

- 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)

bar, barh

df = pd.read_csv('https://raw.githubusercontent.com/kalilurrahman/datasets/main/mobilephonemktshare2020.csv')
df
Date Samsung Apple Huawei Xiaomi Oppo Mobicel Motorola LG Others Realme Google Nokia Lenovo OnePlus Sony Asus
0 2019-10 31.49 22.09 10.02 7.79 4.10 3.15 2.41 2.40 9.51 0.54 2.35 0.95 0.96 0.70 0.84 0.74
1 2019-11 31.36 22.90 10.18 8.16 4.42 3.41 2.40 2.40 9.10 0.78 0.66 0.97 0.97 0.73 0.83 0.75
2 2019-12 31.37 24.79 9.95 7.73 4.23 3.19 2.50 2.54 8.13 0.84 0.75 0.90 0.87 0.74 0.77 0.70
3 2020-01 31.29 24.76 10.61 8.10 4.25 3.02 2.42 2.40 7.55 0.88 0.69 0.88 0.86 0.79 0.80 0.69
4 2020-02 30.91 25.89 10.98 7.80 4.31 2.89 2.36 2.34 7.06 0.89 0.70 0.81 0.77 0.78 0.80 0.69
5 2020-03 30.80 27.03 10.70 7.70 4.30 2.87 2.35 2.28 6.63 0.93 0.73 0.72 0.74 0.78 0.76 0.66
6 2020-04 30.41 28.79 10.28 7.60 4.20 2.75 2.51 2.28 5.84 0.90 0.75 0.69 0.71 0.80 0.76 0.70
7 2020-05 30.18 26.72 10.39 8.36 4.70 3.12 2.46 2.19 6.31 1.04 0.70 0.73 0.77 0.81 0.78 0.76
8 2020-06 31.06 25.26 10.69 8.55 4.65 3.18 2.57 2.11 6.39 1.04 0.68 0.74 0.75 0.77 0.78 0.75
9 2020-07 30.95 24.82 10.75 8.94 4.69 3.46 2.45 2.03 6.41 1.13 0.65 0.76 0.74 0.76 0.75 0.72
10 2020-08 31.04 25.15 10.73 8.90 4.69 3.38 2.39 1.96 6.31 1.18 0.63 0.74 0.72 0.75 0.73 0.70
11 2020-09 30.57 24.98 10.58 9.49 4.94 3.50 2.27 1.88 6.12 1.45 0.63 0.74 0.67 0.81 0.69 0.67
12 2020-10 30.25 26.53 10.44 9.67 4.83 2.54 2.21 1.79 6.04 1.55 0.63 0.69 0.65 0.85 0.67 0.64
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()