日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python时间序列动图_python中如何用matlibplot画时间序列图?

發(fā)布時(shí)間:2025/3/19 python 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python时间序列动图_python中如何用matlibplot画时间序列图? 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

學(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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。