Python 用matplotlib畫以時(shí)間日期為x軸的圖像
1.效果展示
主要效果就是,x軸
顯示時(shí)間單位。
下圖展示的就是想要到達(dá)的效果。
其實(shí)主要是運(yùn)用了datetime.date
這個(gè)類型的變量作為x軸
坐標(biāo)的數(shù)據(jù)輸入。
2. 源碼
將data.txt
中的數(shù)據(jù)讀入,用matplotlib
中的pyplot
畫出,x軸
為時(shí)間。
數(shù)據(jù)文本 data.txt,除了第一行表頭外,每一列都用制表符Tab
(\t
)隔開。
原創(chuàng) 粉絲 喜歡 評(píng)論 等級(jí) 訪問 積分 排名 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行開始計(jì)數(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í)間與上一行的時(shí)間相等,刪除上一行數(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') # 只畫最后一個(gè)元素點(diǎn) - 數(shù)據(jù)點(diǎn)在文字的↘右下,文字在↖左上 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') # 只畫最后一個(gè)元素點(diǎn) - 數(shù)據(jù)點(diǎn)在文字的↗右上,文字在↙左下 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') # 只畫最后一個(gè)元素點(diǎn) - 數(shù)據(jù)點(diǎn)在文字的↘右下,文字在↖左上 plt.text(xs[-1], l_score[-1], l_score[-1], ha='right', va='bottom', fontsize=10) plt.gcf().autofmt_xdate() # 自動(dòng)旋轉(zhuǎn)日期標(biāo)記 # show plt.show()
3. 分析
主要就是matplotlib.pyplot()
可以支持datatime.date
類型的變量。
datetime.strptime(str, '%Y/%m/%d').date()
在shell里的運(yùn)行情況:
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
。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python使用pip安裝模塊出現(xiàn)ReadTimeoutError: HTTPSConnectionPool的解決方法
這篇文章主要介紹了python使用pip安裝模塊出現(xiàn)ReadTimeoutError: HTTPSConnectionPool的解決方法,需要的朋友可以參考下2019-10-10基于python和flask實(shí)現(xiàn)http接口過程解析
這篇文章主要介紹了基于python和flask實(shí)現(xiàn)http接口過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06打印出python 當(dāng)前全局變量和入口參數(shù)的所有屬性
打印出python 當(dāng)前全局變量和入口參數(shù)的所有屬性的實(shí)現(xiàn)代碼。2009-07-07pycharm debug 斷點(diǎn)調(diào)試心得分享
這篇文章主要介紹了pycharm debug 斷點(diǎn)調(diào)試心得分享,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-04-04PyTorch實(shí)現(xiàn)手寫數(shù)字的識(shí)別入門小白教程
這篇文章主要介紹了python實(shí)現(xiàn)手寫數(shù)字識(shí)別,非常適合小白入門學(xué)習(xí),本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06基于注解實(shí)現(xiàn) SpringBoot 接口防刷的方法
這篇文章主要介紹了基于注解實(shí)現(xiàn) SpringBoot 接口防刷的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03解決Python pandas plot輸出圖形中顯示中文亂碼問題
今天小編就為大家分享一篇解決Python pandas plot輸出圖形中顯示中文亂碼問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12