matplotlib 绘制收盘价折线图

aapl.csv

AAPL,28-01-2011, ,344.17,344.4,333.53,336.1,21144800
AAPL,31-01-2011, ,335.8,340.04,334.3,339.32,13473000
AAPL,01-02-2011, ,341.3,345.65,340.98,345.03,15236800
AAPL,02-02-2011, ,344.45,345.25,343.55,344.32,9242600
AAPL,03-02-2011, ,343.8,344.24,338.55,343.44,14064100
AAPL,04-02-2011, ,343.61,346.7,343.51,346.5,11494200
AAPL,07-02-2011, ,347.89,353.25,347.64,351.88,17322100
AAPL,08-02-2011, ,353.68,355.52,352.15,355.2,13608500
AAPL,09-02-2011, ,355.19,359,354.87,358.16,17240800
AAPL,10-02-2011, ,357.39,360,348,354.54,33162400
AAPL,11-02-2011, ,354.75,357.8,353.54,356.85,13127500
AAPL,14-02-2011, ,356.79,359.48,356.71,359.18,11086200
AAPL,15-02-2011, ,359.19,359.97,357.55,359.9,10149000
AAPL,16-02-2011, ,360.8,364.9,360.5,363.13,17184100
AAPL,17-02-2011, ,357.1,360.27,356.52,358.3,18949000
AAPL,18-02-2011, ,358.21,359.5,349.52,350.56,29144500
AAPL,22-02-2011, ,342.05,345.4,337.72,338.61,31162200
AAPL,23-02-2011, ,338.77,344.64,338.61,342.62,23994700
AAPL,24-02-2011, ,344.02,345.15,338.37,342.88,17853500
AAPL,25-02-2011, ,345.29,348.43,344.8,348.16,13572000
AAPL,28-02-2011, ,351.21,355.05,351.12,353.21,14395400
AAPL,01-03-2011, ,355.47,355.72,347.68,349.31,16290300
AAPL,02-03-2011, ,349.96,354.35,348.4,352.12,21521000
AAPL,03-03-2011, ,357.2,359.79,355.92,359.56,17885200
AAPL,04-03-2011, ,360.07,360.29,357.75,360,16188000
AAPL,07-03-2011, ,361.11,361.67,351.31,355.36,19504300
AAPL,08-03-2011, ,354.91,357.4,352.25,355.76,12718000
AAPL,09-03-2011, ,354.69,354.76,350.6,352.47,16192700
AAPL,10-03-2011, ,349.69,349.77,344.9,346.67,18138800
AAPL,11-03-2011, ,345.4,352.32,345,351.99,16824200

python

import numpy as np
import matplotlib.pyplot as mp
import datetime as dt
import matplotlib.dates as md


class StockAnaly(object):
    """分析文件aapl.csv中的股价"""

    def __init__(self):
        """载入aapl.csv中的数据"""

        # 加载文件
        # datas类型为<class 'list'>
        # datas格式为[array(), array(), array(), array(), array()]
        datas = np.loadtxt(
            'aapl.csv',
            usecols=(1,3,4,5,6),
            unpack=True,
            dtype='M8[D], f8, f8, f8, f8',
            delimiter=',',
            converters={1:self.dmy2ymd}
            )

        # 提取数据
        # 类型为<class 'numpy.ndarray'>
        self.dates = datas[0]
        self.opening_prices = datas[1]
        self.highest_prices = datas[2]
        self.lowest_prices = datas[3]
        self.closing_prices = datas[4]   

    def dmy2ymd(self, dmy):
        """将aapl.csv中日期转为'2011-01-28'格式"""

        # 将字节串 b'28-01-2011' 转为字符串
        dmy = str(dmy, encoding='utf-8')
        # 解析字符串,构建日期时间对象
        t = dt.datetime.strptime(dmy, '%d-%m-%Y')
        # 将日期时间对象格式化为字符串 '2011-01-28'
        return t.strftime('%Y-%m-%d')

    def draw_axis(self):
        """绘制坐标轴"""
        mp.figure('AAPL', facecolor='lightgray')
        mp.title('AAPL', fontsize=14)
        mp.xlabel('Date', fontsize=12)
        mp.ylabel('Price', fontsize=12)
        mp.tick_params(labelsize=10)
        mp.grid(linestyle=':')

    def draw_closing_prices_plot(self):
        """绘制收盘价折线"""
        mp.plot(
            self.dates,
            self.closing_prices,
            c='dodgerblue',
            linestyle='--',
            linewidth=3,
            label='AAPL'
            )

    def set_locator(self):
        """设置主刻度、次刻度定位器"""

        # 获取坐标轴对象
        ax = mp.gca()
        # 设置主刻度定位器,每周一为一个主刻度
        major_loc = md.WeekdayLocator(byweekday=md.MO)
        ax.xaxis.set_major_locator(major_loc)
        ax.xaxis.set_major_formatter(md.DateFormatter('%Y-%m-%d'))
        # 格式化日期刻度
        mp.gcf().autofmt_xdate()
        # 设置次刻度定位器,每天为一个次刻度
        minor_loc = md.DayLocator()
        ax.xaxis.set_minor_locator(minor_loc)

    def main(self):
        self.draw_axis()
        self.draw_closing_prices_plot()
        self.set_locator()
        mp.legend()
        mp.show()


if __name__ == '__main__':
    analyzer = StockAnaly()
    analyzer.main()

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值