Python 绘制 K 线图

import mplfinance as mpf
import pandas as pd

# 示例数据
data = pd.DataFrame({
    'Date':  ['2023-01-01', '2023-01-02', '2023-01-03', '2023-01-04', '2023-01-05'],
    'Open':  [100, 102, 104, 103, 105],
    'High':  [105, 107, 106, 108, 109],
    'Low':   [99, 101, 103, 102, 104],
    'Close': [104, 105, 103, 107, 108]
})

# 将日期转换为 DatetimeIndex 格式
data.index = pd.DatetimeIndex(data['Date'])

# 绘制 K 线图
mpf.plot(data,type='candle')

image

也可以提供 style 命名参数来改变 K 线图的风格:

mpf.plot(data,style='yahoo',type='candle')

image

参考:

Dependencies:

mplfinance==0.12.10b0
pandas==2.2.2
posted @ 2024-06-19 02:26  Undefined443  阅读(55)  评论(0编辑  收藏  举报