matplotlib绘制K线图、成交量的组合图

参考:https://blog.csdn.net/u014281392/article/details/73611624

import matplotlib.pyplot as plt ## 导入画图模块
from matplotlib.pylab import date2num ## 导入日期到数值一一对应的转换工具
from dateutil.parser import parse ## 导入转换到指定格式日期的工具
import mpl_finance as mpf ## 导入 mpl_finance 模块
import tushare as ts
from matplotlib.pylab import date2num
import datetime
code = '000001'
wdyx = ts.get_k_data(code,start='2019-10-01', end='2020-01-01')
#         日期,   开盘,     收盘,    最高,      最低,   成交量,    代码
print(wdyx[:3])

def date_to_num(dates):
    num_time = []
    for date in dates:
        date_time = datetime.datetime.strptime(date,'%Y-%m-%d')
        num_date = date2num(date_time)
        num_time.append(num_date)
    return num_time
# dataframe转换为二维数组
mat_wdyx = wdyx.iloc[:,:].values
num_time = date_to_num(mat_wdyx[:,0])
mat_wdyx[:,0] = num_time
print(mat_wdyx[:3])
# 主要参数介绍:
# ax:Axes(轴)对象或Axes(轴)对象数组
# quotes:是传入的数据,数据的前五列要保持和函数规定的一样
# colorup:表示收盘价格>=开盘价格,其图表设置的颜色
# colordown:表示收盘价格<开盘价格,其图表设置的颜色
fig, (ax1, ax2) = plt.subplots(2,figsize=(15,8))
mpf.candlestick_ochl(ax1, mat_wdyx, width=0.6, colorup='g', colordown='r', alpha=1.0)
# 设置日期刻度旋转的角度
# ax1.xticks(rotation=30)
ax1.set_title(code)
ax1.set_ylabel('Price')
ax1.grid(True)
# x轴的刻度为日期
ax1.xaxis_date ()
plt.bar(mat_wdyx[:,0]-0.25, mat_wdyx[:,5], width= 0.5)
ax2.set_ylabel('Volume')
# x轴的刻度为日期
ax2.xaxis_date()
ax2.grid(True)
plt.show()

猜你喜欢

转载自www.cnblogs.com/-wenli/p/12276000.html