原創文章,歡迎轉載。轉載請註明:轉載自 祥的博客
原文鏈接:https://blog.csdn.net/humanking7/article/details/80802435
1.效果展示
主要效果就是,x軸
顯示時間單位。
下圖展示的就是想要到達的效果。
其實主要是運用了datetime.date
這個類型的變量作爲x軸
座標的數據輸入。
2. 源碼
將data.txt
中的數據讀入,用matplotlib
中的pyplot
畫出,x軸
爲時間。
數據文本 data.txt,除了第一行表頭外,每一列都用製表符Tab
(\t
)隔開。
原創 粉絲 喜歡 評論 等級 訪問 積分 排名
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__':
# 打開文本文件 讀取數據
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)
# ################
# 整理數據
# ################
for i in range(1,num):
line = data_lines[i]#從第1行開始[0行開始計數]
if len(line) < 2:
continue #這行明顯不是有效信息
data = line.split('\t')
time = data[0]
# 使用最新日期的數據
if len(l_time) != 0:
if time == l_time[-1]:#如果這一行時間與上一行的時間相等,刪除上一行數據
print('刪除上一行:' + time)
l_time.pop(-1) #刪除上一行記錄的數據
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類型的數據轉換爲datetime.date類型的數據,作爲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')
# 只畫最後一個元素點 - 數據點在文字的↘右下,文字在↖左上
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')
# 只畫最後一個元素點 - 數據點在文字的↗右上,文字在↙左下
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')
# 只畫最後一個元素點 - 數據點在文字的↘右下,文字在↖左上
plt.text(xs[-1], l_score[-1], l_score[-1], ha='right', va='bottom', fontsize=10)
plt.gcf().autofmt_xdate() # 自動旋轉日期標記
# 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
。