numpy常用函数
加载文件要结合numpy库,操作更方便。
加载文件
dates, opening_prices = np.loadtxt(
'../xxxx/aapl.csv', # 文件路径
delimiter=',', # 分隔符
unpack=True, # 是否拆包
usecols=(1,3), # 需要读取哪些列
dtype='U10, f8', # 设置每列的数据类型
converters={1:func} # 数据转换器
)
案例:
"""
demo01_k.py k线图
"""
import numpy as np
import matplotlib.pyplot as mp
import datetime as dt
import matplotlib.dates as md
# 当numpy解析文本时,将会把第一列中的每个字符串
# 都传给函数进行处理, 将处理完毕后的返回值
# 转成需要的M8[D]类型
def dmy2ymd(dmy):
dmy = str(dmy, encoding='utf-8')
# 把dmy转成日期对象
d = dt.datetime.strptime(dmy, '%d-%m-%Y')
t = d.date()
s = t.strftime('%Y-%m-%d')
return s
# 加载文件
dates, opening_prices, highest_prices, \
lowest_prices, closing_prices = np.loadtxt(
'../da_data/aapl.csv', delimiter=',',
usecols=(1,3,4,5,6), unpack=True,
dtype='M8[D], f8, f8, f8, f8',
converters={1:dmy2ymd})
# 绘制收盘价
mp.figure('AAPL', facecolor='lightgray')
mp.title('AAPL', fontsize=18)
mp.xlabel('Date', fontsize=14)
mp.ylabel('Price', fontsize=14)
mp.tick_params(labelsize=10)
mp.grid(linestyle=':')
# 设置主刻度定位器为每周一
ax = mp.gca()
ax.xaxis.set_major_locator(
md.WeekdayLocator(byweekday=md.MO))
ax.xaxis.set_major_formatter(
md.DateFormatter('%Y/%m/%d'))
# 把M8[D]转为matplotlib识别的date类型
dates = dates.astype(md.datetime.datetime)
mp.plot(dates, closing_prices, alpha=0.3,
color='dodgerblue', linewidth=2,
linestyle='--', label='closing_prices')
# 绘制K线图
# 研究颜色的设置
rise = closing_prices >= opening_prices
# 填充色:
color = np.array([('white' if x else 'green') \
for x in rise])
# 边缘色:
ecolor = np.array([('red' if x else 'green') \
for x in rise])
# 绘制影线
# mp.bar(dates, highest_prices-lowest_prices,
# 0.1, lowest_prices, color=ecolor)
mp.vlines(dates, lowest_prices, highest_prices,
color=ecolor, linewidth=0.8)
# 绘制实体
mp.bar(dates, closing_prices-opening_prices,
0.8, opening_prices, color=color,
edgecolor=ecolor, zorder=3)
mp.legend()
# 自动格式化x轴的日期输出
mp.gcf().autofmt_xdate()
mp.show()