美文网首页
Matplotlib Time Series(matplotli

Matplotlib Time Series(matplotli

作者: 呢嘻嘻嘻嘻嘻 | 来源:发表于2019-04-30 14:23 被阅读0次

    Code from matplotlib doc

    根据需求添加了内容,下次随用随取

    '''
    
    Time Series plt show  
    samples format: dataframe 
      data['date', 'CBCFI']
      e.g.: 2014-4-28, 715.59
    '''
    import matplotlib.dates as mdates
    from datetime import datetime
    import matplotlib.cbook as cbook
    import matplotlib.ticker as ticker
    fig, axes = plt.subplots( figsize=(12, 6))
    N = len(data)
    ind = np.arange(N)  #数据索引
    
    '''
    font
    '''
    font = {'family' : 'Times New Roman',
    'weight' : 'normal',
    'size'   : 14,}
    
    date = data.date.astype('O')
    
    test_date = data[(data.date == '2019-1-14')].index.tolist()[0]  
    val_date = data[(data.date == '2018-7-26')].index.tolist()[0]  
    
    def format_date(x, pos=None):
        thisind = np.clip(int(x + 0.5), 0, N - 1)
        return date[thisind].strftime('%Y-%m-%d')  #转换时间格式
    
    ax = axes
    ax.plot(ind, data.CBCFI, '-' ,linewidth=1)
    ax.xaxis.set_major_formatter(ticker.FuncFormatter(format_date))
    ax.set_title("CBCFI History Values",font1)
    fig.autofmt_xdate()  #斜着显示时间戳
    
    '''
    设置刻度值的font
    '''
    plt.tick_params(labelsize=12)
    labels = ax.get_xticklabels() + ax.get_yticklabels()
    [label.set_fontname('Times New Roman') for label in labels]
    
    '''
    插入竖线
    '''
    axes.axvline(x=val_date, color='g', linewidth=1,linestyle = '-.')
    axes.axvline(x=test_date, color='r', linewidth=1,linestyle = ':')
    
    '''
    设置横纵坐标的font
    '''
    plt.ylabel('Index Value', font)
    plt.xlabel('Time',font)
    plt.autoscale(enable=True, axis='both')
    
    '''
    设置图例font
    '''
    #plt.legend(prop=font)
    
    plt.savefig('F:DataFolder/coal/CBCFI.jpg')
    plt.show()
    
    #显示中文
    import matplotlib.pyplot as plt
    
    plt.xlabel("x轴") 
    plt.ylabel("y轴", fontproperties="SimSun") # 步骤一    (宋体)
    plt.title("标题", fontproperties="SimHei") #          (黑体)
    plt.show()
    
    显示效果

    相关文章

      网友评论

          本文标题:Matplotlib Time Series(matplotli

          本文链接:https://www.haomeiwen.com/subject/rifvnqtx.html