• Python 时间序列作图及注释


     一、时间序列作图

     对时间序列作图时,如果横坐标刻度为日期数据的话,利用Matplotlib.pyplot作出的图并不理想(不连续)。例如,对如下格式数据进行画图:

       

       

         

         可以看出,上图效果并不理想。将横轴刻度改为数字。

         

    二、对时间序列进行注释

          若要对上图的其中一些数据进行注释,因为横轴刻度为数字,不是日期,因此在注释上存在一些难度。下面的数据为上图数据的一部分。

           

       两者间只有日期索引是相同的,因此想办法将上图中数据的日期索引改为数字索引,注意两者的数字索引应一致,才能根据数字索引找到对应的数据。关键在于将数据的索引转为list格式。这样日期和数字索引就对应上了。

           

        这样,通过日期就能找到对应的数字数字索引。

        

        好了,就讲到这里吧,下面放上一段注释及画图的代码:

    import datetime
    timedelta1 = datetime.timedelta(days=2)
    timedelta2 = datetime.timedelta(minutes=10)
    for i in range(0,len(df['open_dt'])):
        plt.figure(i)
        plt.figure(figsize=(15,6))
        ax = plt.subplot(111)
        jiacha = data['jiacha'][(pd.to_datetime(df['open_dt'][i]) - timedelta1)
        :(pd.to_datetime(df['close_dt'][i])+timedelta1)]
        ix = list(jiacha.index)
        jiacha1 = np.array(jiacha)
        jiacha1 = pd.Series(jiacha1)
        plt.plot(jiacha1)
        plt.annotate('open',xy=(ix.index(pd.to_datetime(df['open_dt'][i])),df['open_zly'][i]-df['open_dy'][i]),
                     xytext=(ix.index(pd.to_datetime(df['open_dt'][i])),df['open_zly'][i]-df['open_dy'][i]+10),
                      arrowprops = dict(facecolor='red',shrink=0.1))
        plt.annotate('close',xy= (ix.index(pd.to_datetime(df['close_dt'][i])),df['close_zly'][i]-df['close_dy'][i]),
                     xytext=(ix.index(pd.to_datetime(df['close_dt'][i])),df['close_zly'][i]-df['close_dy'][i]+10),
                     arrowprops = dict(facecolor='black',shrink=0.1))
        plt.show() 

  • 相关阅读:
    符号表
    经典算法-堆排序
    AngularJs基础
    Echars详解
    经典算法-快速排序
    经典算法-递归
    异常分类,异常抛出位置
    js的数据类型具体分析
    函数
    数据
  • 原文地址:https://www.cnblogs.com/zhangshuwen/p/6383142.html
Copyright © 2020-2023  润新知