Presentation is loading. Please wait.

Presentation is loading. Please wait.

Matplotlib 파이썬을 활용한 금융 데이터 분석 기초 및 심화 과정 1 1.

Similar presentations


Presentation on theme: "Matplotlib 파이썬을 활용한 금융 데이터 분석 기초 및 심화 과정 1 1."— Presentation transcript:

1 Matplotlib 파이썬을 활용한 금융 데이터 분석 기초 및 심화 과정 1 1

2 Matplotlib Matplotlib is a Python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms pyplot It provides a MATLAB-like plotting framework

3 간단한 그래프 그리기 04_Visualization/01.py import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4]) plt.show()

4 x 축과 y 축 04_Visualization/02.py import matplotlib.pyplot as plt
x = range(0, 100) y = [v*v for v in x] plt.plot(x, y) plt.show()

5 Figure and subplots 04_Visualization/03.py
import matplotlib.pyplot as plt fig = plt.figure() ax1 = fig.add_subplot(2, 1, 1) ax2 = fig.add_subplot(2, 1, 2) x = range(0, 100) y = [v*v for v in x] ax1.plot(x, y) ax2.bar(x, y) plt.show()

6 Figure and subplots 04_Visualization/04.py
import matplotlib.pyplot as plt import numpy as np fig = plt.figure() ax1 = fig.add_subplot(2, 1, 1) # 2x1 중 첫 번째 ax2 = fig.add_subplot(2, 1, 2) # 2x1 중 두 번째 x = np.arange(0.0, 2*np.pi, 0.1) sin_y = np.sin(x) cos_y = np.cos(x) ax1.plot(x, sin_y) ax2.plot(x, cos_y) plt.show()

7 라벨 04_Visualization/05.py ax1.set_xlabel('x') ax1.set_ylabel('sin(x)')
ax2.set_ylabel('cos(x)')

8 범례

9 범례 04_Visualization/06.py import pandas_datareader.data as web
import matplotlib.pyplot as plt lg = web.DataReader("KRX:066570", "google") samsung = web.DataReader("KRX:005930", "google") plt.plot(lg.index, lg['Close'], label='LG Electronics') plt.plot(samsung.index, samsung['Close'], label='Samsung Electronics') plt.legend(loc="best") plt.show()

10 Figure 크기 04_Visualization/07.py import pandas_datareader.data as web
import matplotlib.pyplot as plt lg = web.DataReader("KRX:066570", "google") samsung = web.DataReader("KRX:005930", "google") fig = plt.figure(figsize=(12, 4)) plt.plot(lg.index, lg['Close'], label='LG Electronics') plt.plot(samsung.index, samsung['Close'], label='Samsung Electronics') plt.legend(loc="best") plt.show()

11 matplotlib 구성 matplotlib 그래프 그리기
Figure 객체와 하나 이상의 subplot(Axes) 객체가 필요함 Axes 객체는 다시 두 개의 Axis 객체를 포함 plt.plot()은 최근에 만들어진 Figure 객체의 subplot에 그래프를 그림 만약 subplot이 없다면 새로 생성

12 matplotlib 구성 import matplotlib.pyplot as plt fig = plt.figure()
plt.show() import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(1, 1, 1) plt.show()

13 matplotlib 구성 subplots() 04_Visualization/08.py
Figure 객체를 생성(figure)하고 여러 개의 AxesSubplot 객체를 생성 (add_subplot)하는 두 가지 작업을 한 번에 처리 import matplotlib.pyplot as plt fig, ax_list = plt.subplots(2, 2) ax_list[0][0].plot([1, 2, 3, 4]) plt.show()

14 gca() ax = plt.gca() fig = plt.figure(figsize=(12, 4))
plt.plot(lg.index, lg['Close'], label='LG Electronics') plt.plot(samsung.index, samsung['Close'], label='Samsung Electronics') plt.legend(loc="best") ax = plt.gca() ax.grid(True) plt.show()

15 수정 종가와 거래량 한번에 그리기

16 subplot2grid import matplotlib.pyplot as plt
fig = plt.figure(figsize=(12, 8)) top_axes = plt.subplot2grid((4,4), (0,0), rowspan=3, colspan=4) bottom_axes = plt.subplot2grid((4,4), (3,0), rowspan=1, colspan=4) plt.show()

17 수정 종가와 거래량 한번에 그리기 04_Visualization/09.py
import matplotlib.pyplot as plt import pandas_datareader.data as web sk_hynix = web.DataReader("KRX:000660", "google") fig = plt.figure(figsize=(12, 8)) top_axes = plt.subplot2grid((4,4), (0,0), rowspan=3, colspan=4) bottom_axes = plt.subplot2grid((4,4), (3,0), rowspan=1, colspan=4) bottom_axes.get_yaxis().get_major_formatter().set_scientific(False) top_axes.plot(sk_hynix.index, sk_hynix['Close'], label='Close') bottom_axes.plot(sk_hynix.index, sk_hynix['Volume']) plt.tight_layout() plt.show()

18 bar 차트

19 bar 차트 한글 설정 import matplotlib.pyplot as plt import numpy as np
from matplotlib import font_manager, rc font_name = font_manager.FontProperties(fname="c:/Windows/Fonts/malgun.ttf").get_name() rc('font', family=font_name)

20 bar 차트 데이터 준비 figure and axes fig = plt.figure(figsize=(12, 8))
industry = ['통신업', '의료정밀', '운수창고업', '의약품', '음식료품', '전기가스업', '서비스업', '전기전자', '종이목재', '증권'] fluctuations = [1.83, 1.30, 1.30, 1.26, 1.06, 0.93, 0.77, 0.68, 0.65, 0.61] fig = plt.figure(figsize=(12, 8)) ax = fig.add_subplot(111)

21 bar 차트 bar 차트 그리기 그래프 그리기 ypos = np.arange(10)
rects = plt.barh(ypos, fluctuations, align='center', height=0.5) plt.yticks(ypos, industry) plt.xlabel('등락률') plt.show()

22 bar 차트 텍스트 출력 for i, rect in enumerate(rects):
ax.text(0.95 * rect.get_width(), rect.get_y() + rect.get_height() / 2.0, str(fluctuations[i]) + '%', ha='right', va='center')

23 bar 차트 04_Visualization/10.py import matplotlib.pyplot as plt
import numpy as np from matplotlib import font_manager, rc font_name = font_manager.FontProperties(fname="c:/Windows/Fonts/malgun.ttf").get_name() rc('font', family=font_name) industry = ['통신업', '의료정밀', '운수창고업', '의약품', '음식료품', '전기가스업', '서비스업', '전기전자', '종이목재', '증권'] fluctuations = [1.83, 1.30, 1.30, 1.26, 1.06, 0.93, 0.77, 0.68, 0.65, 0.61] fig = plt.figure(figsize=(12, 8)) ax = fig.add_subplot(111) ypos = np.arange(10) rects = plt.barh(ypos, fluctuations, align='center', height=0.5) plt.yticks(ypos, industry) for i, rect in enumerate(rects): ax.text(0.95 * rect.get_width(), rect.get_y() + rect.get_height() / 2.0, str(fluctuations[i]) + '%', ha='right', va='center') plt.xlabel('등락률') plt.show()

24 음수 처리 한글 폰트 사용시 축에 음수가 나오는 경우 한글 깨지는 경우가 발생함 import matplotlib
matplotlib.rcParams['axes.unicode_minus']=False

25 pie 차트

26 pie 차트 import matplotlib.pyplot as plt
from matplotlib import font_manager, rc # 한글 폰트 font_name = font_manager.FontProperties(fname="c:/Windows/Fonts/malgun.ttf").get_name() rc('font', family=font_name) colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue', 'red'] labels = ['삼성전자', 'SK하이닉스', 'LG전자', '네이버', '카카오'] ratio = [50, 20, 10, 10, 10] explode = (0.0, 0.1, 0.0, 0.0, 0.0) plt.pie(ratio, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=90) plt.show()


Download ppt "Matplotlib 파이썬을 활용한 금융 데이터 분석 기초 및 심화 과정 1 1."

Similar presentations


Ads by Google