Python tushare latest version of mplfinance stock candle chart drawing

Preface: After learning to learn, you learned to draw stock graphics. I tried it. Getting started is pretty simple. I won't know later. Now it seems that mplfinance has replaced the new version, and the old version is not supported. The following code can realize the graphical output of a single stock candlestick chart, but I use the tushare.pro version, which requires points. The code is as follows:

import numpy as np
import pandas as pd
import tushare as ts
import mplfinance as mpf
import matplotlib.pyplot as plt
from pylab import mpl
from datetime import datetime

#pd.set_option()就是pycharm输出控制显示的设置
pd.set_option('expand_frame_repr', False)#True就是可以换行显示。设置成False的时候不允许换行
pd.set_option('display.max_columns', None)# 显示所有列
#pd.set_option('display.max_rows', None)# 显示所有行
pd.set_option('colheader_justify', 'centre')# 显示居中

pro = ts.pro_api('要到tushare官网注册个账户然后将token复制到这里,可以的话请帮个忙用文章末我分享的链接注册,谢谢')
mpl.rcParams['font.sans-serif'] = ['SimHei']  # 指定默认字体
mpl.rcParams['axes.unicode_minus'] = False  # 解决保存图像是负号'-'显示为方块的问题
df = pro.daily(ts_code='000001.SZ', start_date='20171001', end_date='20200801')
#df.sort_values(by='trade_date',ascending=False)
data = df.loc[:, ['trade_date', 'open', 'close', 'high', 'low', 'vol']]  #:取所有行数据,后面取date列,open列等数据
data = data.rename(columns={
    
    'trade_date': 'Date', 'open': 'Open', 'close': 'Close', 'high': 'High', 'low': 'Low', 'vol': 'Volume'})  #更换列名,为后面函数变量做准备
data.set_index('Date', inplace=True)  #设置date列为索引,覆盖原来索引,这个时候索引还是 object 类型,就是字符串类型。
data.index = pd.DatetimeIndex(data.index)  #将object类型转化成 DateIndex 类型,pd.DatetimeIndex 是把某一列进行转换,同时把该列的数据设置为索引 index。
data = data.sort_index(ascending=True)  #将时间顺序升序,符合时间序列
mpf.plot(data, type='candle', mav=(5, 10, 20), volume=True, show_nontrading=False)

Tushare registration link: link

Guess you like

Origin blog.csdn.net/Wilburzzz/article/details/107792381