Python 用matplotlib畫以時間日期為x軸的圖像
1.效果展示
主要效果就是,x軸 顯示時間單位。
下圖展示的就是想要到達的效果。
其實主要是運用了datetime.date這個類型的變量作為x軸坐標的數(shù)據(jù)輸入。

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坐標,將str類型的數(shù)據(jù)轉(zhuǎn)換為datetime.date類型的數(shù)據(jù),作為x坐標
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)日期標記
# 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。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
python使用pip安裝模塊出現(xiàn)ReadTimeoutError: HTTPSConnectionPool的解決方法
這篇文章主要介紹了python使用pip安裝模塊出現(xiàn)ReadTimeoutError: HTTPSConnectionPool的解決方法,需要的朋友可以參考下2019-10-10
基于python和flask實現(xiàn)http接口過程解析
這篇文章主要介紹了基于python和flask實現(xiàn)http接口過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-06-06
打印出python 當前全局變量和入口參數(shù)的所有屬性
打印出python 當前全局變量和入口參數(shù)的所有屬性的實現(xiàn)代碼。2009-07-07
PyTorch實現(xiàn)手寫數(shù)字的識別入門小白教程
這篇文章主要介紹了python實現(xiàn)手寫數(shù)字識別,非常適合小白入門學習,本文通過實例圖文相結(jié)合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06
基于注解實現(xiàn) SpringBoot 接口防刷的方法
這篇文章主要介紹了基于注解實現(xiàn) SpringBoot 接口防刷的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03
解決Python pandas plot輸出圖形中顯示中文亂碼問題
今天小編就為大家分享一篇解決Python pandas plot輸出圖形中顯示中文亂碼問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12

