티스토리 뷰

python

matplotlib 그래프 그리기

swanB 2016. 11. 3. 09:55

import matplotlib.pyplot as plt


#---------------단순 차트---------------

plt.plot( [1,2,3,4])


plt.plot(Series x, Series y)



#---------------복수 차트---------------

fig = plt.figure()


ax1 = fig.add_subplot(1,2,1)

ax2 = fig.add_subplot(1,2,2)


#---------------크기가 다른 2 차트 ---------------


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['Adj Close'], label='Adjusted Close')

Out[110]: [<matplotlib.lines.Line2D at 0xd1b93d0>]


bottom_axes.plot(sk_hynix.index, sk_hynix['Volume'])

Out[111]: [<matplotlib.lines.Line2D at 0xd1b18b0>]


plt.tight_layout()

plt.show()


#---------------값 캡션 --------------


from matplotlib import font_manager, rc

from matplotlib import style


font_name = font_manager.FontProperties(fname="c:/Windows/Fonts/malgun.ttf").get_name()

rc('font', family=font_name)

style.use('ggplot')


industry = ['통신업', '의료정밀', '운수창고업', '의약품', '음식료품', '전기가스업', '서비스업', '전기전자', '종이목재', '증권']

fluctuations = [1.83, 1.30, 1.30, 1.26, 1.06, 0.93, 0.77, 0.68, 0.65, 0.61]

explode = (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.2, 0.0, 0.0)


fig = plt.figure(figsize=(12, 8))

ax = fig.add_subplot(111)


plt.pie(fluctuations, explode = explode, labels = industry, shadow=True, startangle=90)

plt.show()



#-----------------bar차트---------------




#-----------------캔들차트--------------


import pandas_datareader.data as web

import datetime

import matplotlib.pyplot as plt

import matplotlib.finance as matfin

import matplotlib.ticker as ticker


start = datetime.datetime(2016, 3, 1)

end = datetime.datetime(2016, 3, 31)

skhynix = web.DataReader("000660.KS", "yahoo", start, end)

#skhynix = skhynix[skhynix['Volume'] > 0]


fig = plt.figure(figsize=(12, 8))

ax = fig.add_subplot(111)


day_list = []

name_list = []

for i, day in enumerate(skhynix.index):

    if day.dayofweek == 0:

        day_list.append(i)

        name_list.append(day.strftime('%Y-%m-%d') + '(Mon)')


ax.xaxis.set_major_locator(ticker.FixedLocator(day_list))

ax.xaxis.set_major_formatter(ticker.FixedFormatter(name_list))


matfin.candlestick2_ohlc(ax, skhynix['Open'], skhynix['High'], skhynix['Low'], skhynix['Close'], width=0.5, colorup='r', colordown='b')

plt.grid()

plt.show()

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함