溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python如何用matplotlib畫以時間日期為x軸的圖像

發(fā)布時間:2021-08-12 14:35:03 來源:億速云 閱讀:1042 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下Python如何用matplotlib畫以時間日期為x軸的圖像,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

1.效果展示

主要效果就是,x軸 顯示時間單位。

下圖展示的就是想要到達的效果。

其實主要是運用了datetime.date這個類型的變量作為x軸坐標(biāo)的數(shù)據(jù)輸入。

Python如何用matplotlib畫以時間日期為x軸的圖像

2. 源碼

data.txt中的數(shù)據(jù)讀入,用matplotlib中的pyplot畫出,x軸為時間。

數(shù)據(jù)文本 data.txt,除了第一行表頭外,每一列都用制表符Tab\t)隔開。

 原創(chuàng) 粉絲 喜歡 評論 等級 訪問 積分  排名
2018/06/01 69 134 266 64 5  309132 3345  12956  
2018/06/05 72 137 267 65 5  312383 3390  12832  
2018/06/10 74 141 268 68 5  316417 3432  12629  
2018/06/11 75 142 269 69 5  317327 3448  12629  
2018/06/14 76 148 270 70 5  319695 3469  12499  
2018/06/15 79 149 278 73 5  320697 3514  12590  

2018/06/23 84 149 278 73 5  325308 3582  12186  
2018/06/24 84 149 278 73 5  325583 3583  12233  
2018/06/25 84 149 278 73 5  326008 3584  12038  
2018/06/25 84 149 279 73 5  326039 3584  12038

程序源碼:

# read csdn data
from datetime import datetime 
import matplotlib.pyplot as plt #引入繪圖庫


if __name__ == '__main__':

  # 打開文本文件 讀取數(shù)據(jù)
  with open("data.txt",'r',encoding='utf-8') as f:
    data_lines = f.readlines()

  l_time  = []
  l_article = []
  l_fans  = []
  l_like  = []
  l_remark = []
  l_level  = []
  l_visit  = []
  l_score  = []
  l_rank  = []

  num = len(data_lines)

  # ################
  #   整理數(shù)據(jù)
  # ################
  for i in range(1,num):
    line = data_lines[i]#從第1行開始[0行開始計數(shù)]
    if len(line) < 2:
      continue  #這行明顯不是有效信息

    data = line.split('\t')
    time = data[0]
    # 使用最新日期的數(shù)據(jù)
    if len(l_time) != 0:
      if time == l_time[-1]:#如果這一行時間與上一行的時間相等,刪除上一行數(shù)據(jù)
        print('刪除上一行:' + time)
        l_time.pop(-1) #刪除上一行記錄的數(shù)據(jù)
        l_article.pop(-1)
        l_fans.pop(-1)
        l_like.pop(-1)
        l_remark.pop(-1)
        l_level.pop(-1)
        l_visit.pop(-1)
        l_score.pop(-1)
        l_rank.pop(-1)


    arti = int(data[1])
    fans = int(data[2])
    like = int(data[3])
    rmak = int(data[4])
    leve = int(data[5])
    visi = int(data[6])
    scor = int(data[7])
    rank = int(data[8])
    l_time.append(time)
    l_article.append(arti)
    l_fans.append(fans)
    l_like.append(like)
    l_remark.append(rmak)
    l_level.append(leve)
    l_visit.append(visi)
    l_score.append(scor)
    l_rank.append(rank)

  # ################
  #    畫圖
  # ################
  # X坐標(biāo),將str類型的數(shù)據(jù)轉(zhuǎn)換為datetime.date類型的數(shù)據(jù),作為x坐標(biāo)
  xs = [datetime.strptime(d, '%Y/%m/%d').date() for d in l_time]

  plt.figure(1)
  plt.subplot(1, 3, 1)
  plt.title('Visit Number')
  plt.plot(xs, l_visit, 'o-')
  plt.xlabel('Time')
  plt.ylabel('Visit Number')

  # 只畫最后一個元素點 - 數(shù)據(jù)點在文字的↘右下,文字在↖左上
  plt.text(xs[-1], l_visit[-1], l_visit[-1], ha='right', va='bottom', fontsize=10)


  plt.subplot(1, 3, 2)
  plt.title('Rank')
  plt.plot(xs, l_rank, 'o-')
  plt.xlabel('Time')
  plt.ylabel('Rank')
  # 只畫最后一個元素點 - 數(shù)據(jù)點在文字的↗右上,文字在↙左下
  plt.text(xs[-1], l_rank[-1], l_rank[-1], ha='right', va='top', fontsize=10)



  plt.subplot(1, 3, 3)
  plt.title('Score')
  plt.plot(xs, l_score, 'o-')
  plt.xlabel('Time')
  plt.ylabel('Score')
  # 只畫最后一個元素點 - 數(shù)據(jù)點在文字的↘右下,文字在↖左上
  plt.text(xs[-1], l_score[-1], l_score[-1], ha='right', va='bottom', fontsize=10)


  plt.gcf().autofmt_xdate() # 自動旋轉(zhuǎn)日期標(biāo)記

  # show
  plt.show()

3. 分析

主要就是matplotlib.pyplot()可以支持datatime.date類型的變量。

datetime.strptime(str, '%Y/%m/%d').date()

在shell里的運行情況:

In [5]: var = datetime.strptime('2018/3/15', '%Y/%m/%d').date()

In [6]: var
Out[6]: datetime.date(2018, 3, 15)

In [7]: type(var)
Out[7]: datetime.date

所以,源碼中變量xs為含有一群datetime.date變量的list

以上是“Python如何用matplotlib畫以時間日期為x軸的圖像”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI