k线图

import tushare as ts
import re
import matplotlib.pyplot as plt
import mpl_finance as mpf
import numpy as np
import talib as tl

api = ts.pro_api()
df = ts.pro_bar('600073.SH', start_date='2019-12-30 00:00:00', end_date='2019-12-30 15:00:00', freq='1min')[::-1]
# 加均线
sma_10 = tl.SMA(np.array(df['close']), 10)
sma_30 = tl.SMA(np.array(df['close']), 30)

index = list(map(lambda i: re.findall(r'd{2}:d{2}', i)[0], df.trade_time))
fig = plt.figure(figsize=(38, 18), dpi=100)
ax = fig.add_subplot(1, 1, 1)

ax.set_title(f'2019-12-27 {600073}', fontsize=30, color='red')
ax.set_xticks(range(0, len(df.index)))
ax.set_xticklabels(index, rotation=-80)

mpf.candlestick2_ochl(ax, df['open'], df['close'], df['high'],
                      df['low'], width=0.6, colorup='r', colordown='g', alpha=0.75)

# 设置字体
plt.rcParams['font.sans-serif'] = ['Microsoft JhengHei']
# 添加图例
ax.plot(sma_10, label='10日均线')
ax.plot(sma_30, label='30日均线')
ax.legend()

plt.savefig(f'./picture/{600073}.png')
plt.show()
print(f'{600073} 存储成功!')

  

原文地址:https://www.cnblogs.com/wangtaobiu/p/12124399.html