zoukankan      html  css  js  c++  java
  • matplotlib画折线图,并以时间作为横轴

    代码:

     1 #!usr/bin/env python
     2 # -*- coding: utf-8 -*-
     3 import os
     4 import numpy as np
     5 import pandas as pd
     6 import matplotlib.pyplot as plt
     7 from datetime import datetime
     8 import matplotlib.dates as mdates
     9 from dateutil import parser
    10 
    11 base_dir = 'd:/Pattern/'
    12 input_base_dir = base_dir + 'data_graph_save/'
    13 
    14 fileName = input_base_dir + 'output_6.csv'
    15 data = pd.read_csv(fileName)
    16 data_date_str = data.iloc[:, 0]
    17 data_date = list(map(parser.parse, data_date_str))
    18 data_cma = data.iloc[:, 1]
    19 data_ecmwf = data.iloc[:, 2]
    20 data_jma = data.iloc[:, 3]
    21 data_ncep = data.iloc[:, 4]
    22 data_mean = data.iloc[:, 5]
    23 data_true = data.iloc[:, 6]
    24 data_predict = data.iloc[:, 7]
    25 
    26 plt.figure(figsize=(13,7), dpi=80)
    27 plt.subplot(1,1,1)
    28 # 绘制余弦曲线,使用蓝色的、连续的、宽度为 1 (像素)的线条
    29 plt.plot(data_date, data_predict, color='blue', linewidth=1.0, linestyle='-', label='predict')
    30 plt.plot(data_date, data_true, color='red', linewidth=1.0, linestyle='-', label='observation')
    31 plt.plot(data_date, data_cma, color='green', linewidth=1.0, linestyle='-', label='babj')
    32 plt.plot(data_date, data_ecmwf, color='yellow', linewidth=1.0, linestyle='-', label='ecmf')
    33 plt.plot(data_date, data_jma, color='purple', linewidth=1.0, linestyle='-', label='rjtd')
    34 plt.plot(data_date, data_ncep, color='orange', linewidth=1.0, linestyle='-', label='kwbc')
    35 plt.plot(data_date, data_mean, color='pink', linewidth=1.0, linestyle='-', label='mean')
    36 # 显示图示
    37 plt.legend()
    38 
    39 
    40 # 配置横坐标
    41 plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%Y'))
    42 plt.gca().xaxis.set_major_locator(mdates.MonthLocator())    # 按月显示,按日显示的话,将MonthLocator()改成DayLocator()
    43 plt.gcf().autofmt_xdate()  # 自动旋转日期标记
    44 plt.title('Temperation constraction')
    45 plt.savefig('temper.png')
    46 plt.show()

    结果如下图所示。

  • 相关阅读:
    从例图中学习思维导图的基本概念
    名企面试官精讲典型编程题之C++篇
    PPT,要你好看(全彩)
    文字即艺术
    MindManager中读图工具的使用
    思维导图的三招十八式
    放之四海皆适用的设计原则(一)
    MindManager的例图资源
    内容营销11金规
    专业嵌入式软件开发——全面走向高质高效编程(含DVD光盘1张)
  • 原文地址:https://www.cnblogs.com/sunshine8/p/8040220.html
Copyright © 2011-2022 走看看