python时间序列动图_python中如何用matlibplot画时间序列图?
學(xué)了編程后,突然發(fā)現(xiàn)用python畫圖是一件很容易的事。想要放松一下的小伙伴,今天我們一起來畫一個(gè)時(shí)間序列圖吧。
一、讀取數(shù)據(jù)及處理
通過pandas讀取CSV文件,keep_default_na參數(shù)將空值數(shù)據(jù)改為空字符串
查看原數(shù)據(jù)信息,原數(shù)據(jù)分為3day, 時(shí)間粒度3min
通過pandas date_range函數(shù)生成時(shí)間序列時(shí)間數(shù)據(jù),指定freq='180s'import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
%matplotlib inline
plt.rcParams['font.sans-serif'] = ['SimHei'] # 顯示中文(windows)
plt.rcParams['axes.unicode_minus'] = False # 用來正常顯示負(fù)號(hào)
df = pd.read_csv('traffic_analysis_macro.csv', keep_default_na=False) # 無數(shù)據(jù)當(dāng)做空字符串處理
# df.drop(['region_id'], axis=1, inplace=True)
# 查看原始數(shù)據(jù)集情況
print('shape:', df.shape)
print('describle:', df.describe())
print('data head:', df.head())
# 該數(shù)據(jù)集,分為3天,時(shí)間粒度3min;
# 首先按天切分?jǐn)?shù)據(jù)
df_0912 = df[:480]
df_0915 = df[480:960]
df_0916 = df[960:]
# 生成時(shí)間序列:X軸刻度數(shù)據(jù)
table = pd.DataFrame([i for i in range(480)],columns=['value'],index=pd.date_range('00:00:00', '23:57:00', freq='180s'))
二、繪制圖形# 圖片大小設(shè)置
fig = plt.figure(figsize=(15,9), dpi=100)
ax = fig.add_subplot(111)
# X軸時(shí)間刻度格式 & 刻度顯示
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
plt.xticks(pd.date_range(table.index[0],table.index[-1],freq='H'), rotation=45)
# 繪圖
ax.plot(table.index,df_0912['avg_speed'],color='r', label='9月12日')
ax.plot(table.index,df_0915['avg_speed'],color='y', label='9月15日')
ax.plot(table.index,df_0916['avg_speed'],color='g', label='9月16日')
# 輔助線
sup_line = [35 for i in range(480)]
ax.plot(table.index, sup_line, color='black', linestyle='--', linewidth='1', label='輔助線')
plt.xlabel('time_point', fontsize=14) # X軸標(biāo)簽
plt.ylabel("Speed", fontsize=16) # Y軸標(biāo)簽
ax.legend() # 圖例
plt.title("車速時(shí)序圖", fontsize=25, color='black', pad=20)
plt.gcf().autofmt_xdate()
# 隱藏-上&右邊線
# ax.spines['right'].set_color('none')
# ax.spines['top'].set_color('none')
# plt.savefig('speed.png')
plt.show()
三、效果圖如下
以上就是python中用matlibplot畫時(shí)間序列圖的方法。更多Python學(xué)習(xí)推薦:PyThon學(xué)習(xí)網(wǎng)教學(xué)中心。
總結(jié)
以上是生活随笔為你收集整理的python时间序列动图_python中如何用matlibplot画时间序列图?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小甲鱼python课后题答案_小甲鱼py
- 下一篇: python分割数字_对python数据