[IT]/python.matplotlib
finance
givemebro
2020. 6. 9. 09:13
반응형
import FinanceDataReader as fdr
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import matplotlib.dates as mdates
import matplotlib.ticker as mticker
from matplotlib import style
from matplotlib import font_manager, rc
from matplotlib import gridspec
import csv
from mpl_finance import candlestick_ohlc
from datetime import date
font_name=font_manager.FontProperties(fname="C:/Windows/Fonts/HMFMPYUN.TTF").get_name()
rc('font',family=font_name)
fig=plt.figure()
ax1=plt.subplot2grid((2,2),(0,0))
ax2=plt.subplot2grid((2,2),(0,1))
ax3=plt.subplot2grid((2,2),(1,0),colspan=2)
def drawBirths():
global ax3
years=range(1880,2012)
pieces=[]
# data 경로 설정
datafile='data/births.txt'
with open(datafile,'rt') as f:
data=csv.reader(f,delimiter=',')
for d in data:
pieces.append(d)
x=[int(year) for year,female,male in pieces]
y1=[int(female) for year,female,male in pieces]
y2=[int(male) for year,female,male in pieces]
ax3.plot(x,y1,'-',color='r',label='여아')
ax3.plot(x,y2,'-',color='b',label='남아')
ax3.grid(True)
ax3.legend(loc=4)
def viewStockTrend_1():
global ax1
df = fdr.DataReader("005930", '2019')
ref_price =43000
date=df.index
opn=np.array(df['Open'].astype(int))
high=np.array(df['High'].astype(int))
low=np.array(df['Low'].astype(int))
closep=np.array(df['Close'].astype(int))
volume=np.array(df['Volume'].astype(int))
change=np.array(df['Change'].astype(int))
ax1.plot([],[],linewidth=5,label='이익',color='r',alpha=0.5)
ax1.plot([],[],linewidth=5,label='손해',color='b',alpha=0.5)
ax1.fill_between(date, closep, ref_price, where=(closep > ref_price),
facecolor='r', alpha=0.5)
ax1.fill_between(date, closep, ref_price, where=(closep < ref_price),
facecolor='b', alpha=0.5)
ax1.spines['left'].set_color('c')
ax1.spines['left'].set_linewidth(5)
ax1.spines['right'].set_visible(False)
ax1.spines['top'].set_visible(False)
ax1.plot_date(date, closep, '-', color='b')
for label in ax1.xaxis.get_ticklabels():
label.set_rotation(45)
ax1.tick_params(axis='x', colors='r')
ax1.tick_params(axis='y', colors='#225588')
ax1.grid(True)
def drawStockTrend2():
global ax2
ref_price = 43000
df = fdr.DataReader("005930", '2015')
arrayData = np.array(df)
#date = df.index.
dates = pd.to_datetime(df.index, format="%Y-%m-%d")
# print(type(date[0]))
closeps = np.array(df['Close']).astype(int)
highs = np.array(df['High']).astype(int)
lows = np.array(df['Low']).astype(int)
openps = np.array(df['Open']).astype(int)
volumes = np.array(df['Volume']).astype(int)
# ax3에 주가 캔들을 그립니다.
ohlc = []
for i in range(len(dates)):
d = mdates.date2num(dates[i])
stock_data = d, openps[i], highs[i], lows[i], closeps[i], volumes[i]
ohlc.append(stock_data)
candlestick_ohlc(ax2, ohlc, width=0.5, colorup='r', colordown='b')
# candlestick_ohlc(ax2, zip(mdates.date2num(dates), df['Open'], df['High'], df['Low'], df['Close']),
# width=0.5, colorup="r", colordown="b")
ax2.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
ax2.xaxis.set_major_locator(mticker.MaxNLocator(10))
for label in ax2.xaxis.get_ticklabels():
label.set_rotation(45)
ax2.grid(True)
drawBirths()
viewStockTrend_1()
drawStockTrend2()
plt.show()
반응형