python画简单K线

import pandas as pd
from matplotlib import pyplot as plt
#import matplotlib.finance as mpf
from mpl_finance import candlestick_ohlc
from matplotlib.pylab import date2num
import datetime

dat = pd.read_csv('data.csv')

quotes =[]
x = []
d = pd.date_range('2019-06-01','2019-06-22')
print(d)
for i in range(0,23):
  code = str(d[1])[:10]
  sdate_num = date2num(datetime.datetime.strptime(code,'%Y-%m-%d'))
  x.append(sdate_num)
  sopen = dat.ix[i, 'open']
  shigh = dat.ix[i, 'high']
  slow = dat.ix[i, 'low']
  sclose = dat.ix[i,'close']
  if i==0:
      nums = sdate_num
  else:
      nums = sdate_num + i
  
  datas = (nums, sopen, shigh, slow, sclose) # 按照 candlestick_ohlc 要求的数据结构准备数据
  quotes.append(datas)

print(quotes)

fig, ax = plt.subplots(facecolor=(0, 0.3, 0.5),figsize=(12,8))
fig.subplots_adjust(bottom=0.1)
ax.xaxis_date()
plt.xticks(rotation=45) #日期显示的旋转角度
plt.title(u'k-line')
plt.xlabel('code')
plt.ylabel('price')
candlestick_ohlc(ax,quotes,width=0.7,colorup='r',colordown='green') # 上涨为红色K线,下跌为绿色,K线宽度为0.7
plt.grid(True)
plt.scatter(range(737212,737235), dat.ix[:,'meandeal'], marker='+', c='b')
plt.scatter(range(737212,737235), dat.ix[:,'deal'], marker='+', c='y')
fig.tight_layout()
plt.show()

data.csv字段:
code股票代码 ,open开盘价,close收盘价,meandeal全天成交均价,high全天最高成交价,low全天最低成交价, deal成交价

k线图.png

你可能感兴趣的:(python画简单K线)