본문 바로가기

Programming

[Python][Library] 3. matplotlib - 1. 시각화

반응형
#!/usr/bin/env python
# coding: utf-8

# # Matplotlib: 시각화

# # 1. Matplotlib API

# In[1]:


import numpy as np
#np.set_printoptions(precision=4, suppress=True)

import pandas as pd
#pd.options.display.max_rows = 20

import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')

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

from matplotlib import rcParams
rcParams['font.family'] = 'NanumGothic'
rcParams['font.size'] = 10
rcParams['axes.unicode_minus'] = False


# In[2]:


from matplotlib import style

# 'ggplot', 'fivethirtyeight', 'dark_background', 'grayscale', 'bmh'
style.use('ggplot')


# #### 한글 폰트 다운로드

# - [구글 폰트 무료 배포 사이트](https://www.google.co.kr/get/noto/#/)
# - Korean 으로 검색
# - 예: 'Noto Sans Korean'

# #### 시스템 폰트 찾기

# In[3]:


import matplotlib.font_manager as fm

for f in fm.fontManager.ttflist:
    if f.name.find('Nanum') != -1:
        print(f.name)


# #### 한글 폰트 설정

# In[2]:


from matplotlib import rcParams

rcParams['font.family'] = 'NanumGothic'
rcParams['font.size'] = 10


# #### 한글 폰트 설정 확인

# In[4]:


fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(range(10))
ax.set_title('한글 제목')
plt.show()


# ### 1.1 Figure와 subplot

# In[ ]:


data = np.arange(10)
data


# In[ ]:


plt.plot(data)


# In[ ]:


plt.plot(np.random.randn(50).cumsum(), 'k--')


# In[ ]:


plt.hist(np.random.randn(100), bins=20, color='k', alpha=0.3)
plt.show()


# #### 여러개의 그림판 생성

# In[ ]:


fig = plt.figure()

ax1 = fig.add_subplot(2, 2, 1)
ax2 = fig.add_subplot(2, 2, 2)
ax3 = fig.add_subplot(2, 2, 3)

ax1.hist(np.random.randn(100), bins=20, color='k', alpha=0.3)

ax2.scatter(np.arange(30), np.arange(30) + 3 * np.random.randn(30))


# #### subplots() 옵션
# 인자 | 설명
# :---|:---
# nrows | 서브플롯의 로우 수
# ncols | 서브플롯의 컬럼 수
# sharex | 모든 서브플롯이 x축 눈금을 공유
# sharey | 모든 서블플롯이 y축 눈금을 공유

# #### 여러개의 그림판 배열로 생성

# In[ ]:


fig, axes = plt.subplots(2, 2, sharex=True, sharey=True)
for i in range(2):
    for j in range(2):
        axes[i, j].hist(np.random.randn(500), bins=50, color='k', alpha=0.5)

# 간격조정        
plt.subplots_adjust(wspace=0, hspace=0)


# ### 1.2 Line Chart

# In[ ]:


from dateutil import parser

kl = pd.read_csv('data/k_league_fw_goals.csv', index_col=0)
lp = pd.read_csv('data/liverpool.csv', index_col=0, date_parser=parser.parse)
cf = pd.read_csv('data/seac_coffee.csv')

coffee = cf[['A', 'C', 'E', 'S']].sum().values
index = np.arange(len(coffee))


# In[ ]:


kl.head()


# In[ ]:


cf.head()


# In[ ]:


lp.head()


# In[ ]:


plt.plot(lp['CumWin'])


# In[ ]:


plt.plot(lp[['CumWin', 'CumLost']])


# In[ ]:


#plt.plot(cf) # ValueError
plt.plot(cf['A'])


# In[ ]:


plt.plot(cf[['A', 'C', 'E', 'S']])


# ### 1.3 Bar Chart

# In[ ]:


# matplotlib.pyplot.bar(left, height, width=0.8, bottom=None, hold=None, **kwargs)
plt.bar(np.arange(4), cf[['A', 'C', 'E', 'S']].sum().values)


# In[ ]:


bar_width = 0.5

plt.bar(index, coffee, width=bar_width, color='r')

plt.title('# Stores by Coffee Shop')

plt.xlabel('Coffee Shop')

plt.ylabel('# Store')

plt.xticks(index + bar_width - 0.25, ('A', 'C', 'E', 'S'))

plt.show()


# In[ ]:


bar_height = 0.5
plt.barh(index, coffee, height=bar_height, color='g')
plt.title('# Stores by Coffee Shop')
plt.ylabel('Coffee Shop')
plt.xlabel('# Store')
plt.yticks(index + bar_width - 0.25, ('A', 'C', 'E', 'S'))

plt.show()


# ### 1.4 Pie Chart

# In[ ]:


labels = ('Angelinus', 'Cafe Bene', 'Ediya', 'Starbucks')
colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral']

plt.pie(coffee, labels=labels, colors=colors,
        autopct='%1.1f%%', shadow=True,
        startangle=135, explode=(0, 0, 0, 0.3))

plt.show()


# ### 1.5 Scatter Chart

# In[ ]:


fig, ax = plt.subplots()

ax.scatter(kl['st'], kl['goal'], alpha=0.5, color='r', marker='*')

ax.set_xlabel('st')
ax.set_ylabel('goal')

ax.set_title('Shoot vs Goal')

ax.grid(True)


# In[ ]:


with plt.xkcd():
    bar_width = 0.5
    bar_height = 0.5
    plt.barh(index, coffee, height=bar_height, color='g')
    plt.title('# Stores by Coffee Shop')
    plt.ylabel('Coffee Shop')
    plt.xlabel('# Store')
    plt.yticks(index + bar_width - 0.25, ('A', 'C', 'E', 'S'))


# ### 1.6 색상, 마커, 선 스타일

# In[ ]:


plt.plot(np.random.randn(30).cumsum(), 'g--')
#plt.plot(np.random.randn(30).cumsum(), 'ko--')


# In[ ]:


plt.plot(np.random.randn(30).cumsum(), color='g', linestyle='--')
#plt.plot(np.random.randn(30).cumsum(), color='k', linestyle='dashed', marker='o')


# In[ ]:


data = np.random.randn(30).cumsum()
plt.plot(data, 'k--', label='Default')
plt.plot(data, 'k-', drawstyle='steps-post', label='steps-post')
plt.legend(loc='best')


# #### Matplotlib 스타일
#
# #### color
#
# * Pre-Defined color
#
# 컬러 기호| 컬러
# :---|:---
# b| blue
# g| green
# r| red
# c| cyan
# m| magenta
# y| yellow
# k| black
# w| white
#
# * Grayscale (0 - 1)
# ```python
# color = '0.75'
# ```
#
# * HTML Hex String
# ```python
# color = '#eeefff'
# ```

# #### line
#
# 선 기호 | 선 모양
# :---|:---
# '-' | solid
# '--' | dashed
# '-.' | dash_dot
# ':' | dotted
# '' | draw nothing

# #### 조합
# - 'k--'
# - 'g-.'
# - 'r:'
# - 'ko--'


# ### 1.7 눈금, 라벨, 범례

# #### 제목 축 이름, 눈금 이름

# In[ ]:


fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(np.random.randn(1000).cumsum())

ax.set_xticks([0, 250, 500, 750, 1000])
ax.set_xticklabels(['one', 'two', 'three', 'four', 'five'],
                            rotation=30, fontsize='small')

ax.set_title('My first matplotlib plot')
ax.set_xlabel('Stages')


# #### 범례 추가

# In[ ]:


fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

ax.plot(np.random.randn(1000).cumsum(), 'k', label='one')
ax.plot(np.random.randn(1000).cumsum(), 'k--', label='two')
ax.plot(np.random.randn(1000).cumsum(), 'k.', label='three')

ax.legend(loc='best')


# ### 1.8 주석, 그림 추가

# #### 주석

# In[ ]:


from datetime import datetime

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

data = pd.read_csv('data/spx.csv', index_col=0, parse_dates=True)
spx = data['SPX']

spx.plot(ax=ax, style='k-')

crisis_data = [
    (datetime(2007, 10, 11), 'Peak of bull market'),
    (datetime(2008, 3, 12), 'Bear Stearns Fails'),
    (datetime(2008, 9, 15), 'Lehman Bankruptcy')
]

for date, label in crisis_data:
    ax.annotate(label, xy=(date, spx.asof(date) + 75),
                xytext=(date, spx.asof(date) + 225),
                arrowprops=dict(facecolor='black', headwidth=4, width=2,
                                headlength=4),
                horizontalalignment='left', verticalalignment='top')

# X축 범위 지정
ax.set_xlim(['1/1/2007', '1/1/2011'])

# Y축 범위 지정
ax.set_ylim([600, 1800])

# 제목 지정
ax.set_title('Important dates in the 2008-2009 financial crisis')


# #### 그림

# In[ ]:


fig = plt.figure(figsize=(12, 6)); ax = fig.add_subplot(1, 1, 1)

rect = plt.Rectangle((0.2, 0.75), 0.4, 0.15, color='k', alpha=0.3)

circ = plt.Circle((0.7, 0.2), 0.15, color='b', alpha=0.3)

pgon = plt.Polygon([[0.15, 0.15], [0.35, 0.4], [0.2, 0.6]], color='g', alpha=0.5)

ax.add_patch(rect)
ax.add_patch(circ)
ax.add_patch(pgon)


# ### 1.9 파일로 저장

# In[ ]:


plt.plot(np.random.randn(50).cumsum(), 'k--')
plt.savefig('data/1.png')
plt.savefig('data/2.png', dpi=400, bbox_inches='tight')


# ---

# # 2. Pandas에서 그래프 그리기

# ### 2.1 선 그래프

# In[ ]:


sr = pd.Series(np.random.randn(10).cumsum(), index=np.arange(0, 100, 10))
sr.plot()


# In[ ]:


df = pd.DataFrame(np.random.randn(10, 4).cumsum(0),
                  columns=['A', 'B', 'C', 'D'],
                  index=np.arange(0, 100, 10))
df.plot()


# #### Series.plot() 인자
#
# 인자 | 설명
# :---|:---
# lable | 그래프의 범례 이름
# style | matplotlib에 전달할 'ko--'같은 스타일의 문자열
# alpha | 그래프 투명도 ( 0 ~ 1 )
# kind | 그래프 종류. 'line', 'bar', 'barh', 'kde'
# use_index | 객체의 색인을 눈금 이름으로 사용할지의 여부
# xticks | X축으로 사용할 값
# yticks | Y축으로 사용할 값
# xlim | X 축 한계
# ylim | Y 축 한계

# #### DataFrame에서만 사용하는 plot() 인자
#
# 인자 | 설명
# :---|:---
# subplots | 각 DataFram의 컬럼을 독립된 서브플롯에 그린다.
# sharex | X축을 공유한다.
# sharey | Y축을 공유한다.
# figsize | 그래프의 크기
# title | 그래프의 제목

# ### 2.2 막대 그래프

# In[ ]:


fig, axes = plt.subplots(2, 1)
data = pd.Series(np.random.rand(16), index=list('abcdefghijklmnop'))

data.plot.bar(ax=axes[0],  color='g', alpha=0.7)

data.plot.barh(ax=axes[1], color='b', alpha=0.7)


# In[ ]:


df = pd.DataFrame(np.random.rand(6, 4),
                  index=['one', 'two', 'three', 'four', 'five', 'six'],
                  columns=pd.Index(['A', 'B', 'C', 'D'], name='Genus'))
df
df.plot.bar()


# In[ ]:


df.plot.barh(stacked=True, alpha=0.5)


# In[ ]:


tips = pd.read_csv('data/tips.csv')

party_counts = pd.crosstab(tips['day'], tips['size'])
party_counts

# Not many 1- and 6-person parties
party_counts = party_counts.loc[:, 2:5]


# In[ ]:


# Normalize: item / sum(item)
party_pcts = party_counts.div(party_counts.sum(axis=1), axis=0)
party_pcts
party_pcts.plot.bar()


# #### <참고> seaborn

# In[ ]:


# !pip install seaborn


# In[ ]:


import seaborn as sns


# In[ ]:


tips['tip_pct'] = tips['tip'] / (tips['total_bill'] - tips['tip'])
tips.head()


# In[ ]:


sns.barplot('tip_pct','day', data=tips, orient='h')
#sns.barplot(x='tip_pct', y='day', data=tips, orient='h')


# In[ ]:


sns.barplot(x='tip_pct', y='day', hue='time', data=tips, orient='h')


# In[ ]:


sns.set(style="whitegrid")


# ### 2.3 히스토그램, 밀도 그래프

# In[ ]:


tips['tip_pct'].plot.hist(bins=50)


# In[ ]:


tips['tip_pct'].plot.density()


# In[ ]:


# seaborn

comp1 = np.random.normal(0, 1, size=200)
comp2 = np.random.normal(10, 2, size=200)

values = pd.Series(np.concatenate([comp1, comp2]))

sns.distplot(values, bins=100, color='k')


# ### 2.4 산포도

# In[ ]:


macro = pd.read_csv('data/macrodata.csv')
data = macro[['cpi', 'm1', 'tbilrate', 'unemp']]

trans_data = np.log(data).diff().dropna()

trans_data[-5:]


# In[ ]:


plt.scatter(trans_data['m1'], trans_data['unemp'])

plt.title('Change in log %s vs. log %s' % ('m1', 'unemp'))


# In[ ]:


pd.plotting.scatter_matrix(trans_data, diagonal='kde', color='k', alpha=0.3)
plt.show()


# In[ ]:


# seaborn

sns.regplot('m1', 'unemp', data=trans_data)
plt.title('Changes in log %s versus log %s' % ('m1', 'unemp'))


# In[ ]:


# seaborn

sns.pairplot(trans_data, diag_kind='kde', plot_kws={'alpha': 0.2})


# ---

# In[ ]:


# end of file


# In[ ]:





반응형
LIST