Files
python----/数据分析/index.ipynb
T
2025-10-20 12:32:18 +08:00

264 KiB
Raw Blame History

In [3]:
with open('network.txt', 'r') as f:
    data = f.read()
In [14]:
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter
plt.rcParams['font.family'] = 'SimHei'  #
In [15]:
df = pd.read_csv('network.csv', sep='\s{2,}',engine='python', 
                 names=['日期', '耗时'])
<>:1: SyntaxWarning: invalid escape sequence '\s'
<>:1: SyntaxWarning: invalid escape sequence '\s'
C:\Users\Administrator\AppData\Local\Temp\ipykernel_28148\4043059997.py:1: SyntaxWarning: invalid escape sequence '\s'
  df = pd.read_csv('network.csv', sep='\s{2,}',engine='python',
In [16]:
df
Out [16]:
日期 耗时
0 2025-05-26 10:20:04 3896
1 2025-05-26 10:30:03 3441
2 2025-05-26 10:31:00 300
3 2025-05-26 10:32:00 305
4 2025-05-26 10:33:00 292
... ... ...
1334 2025-05-27 08:48:01 1547
1335 2025-05-27 08:49:00 404
1336 2025-05-27 08:50:00 479
1337 2025-05-27 08:51:00 931
1338 2025-05-27 08:52:00 325

1339 rows × 2 columns

In [ ]:
df = pd.read_csv('network.csv', sep='\s{2,}',engine='python', 
                 names=['日期', '耗时'])
# 转换数据类型
df['日期'] = pd.to_datetime(df['日期'])
df['耗时'] = df['耗时']   # 转换为秒

# 创建图表
plt.figure(figsize=(16, 9))
plt.plot(df['日期'], df['耗时'],  linestyle='-')

# 设置时间格式
date_format = DateFormatter('%H:%M')
plt.gca().xaxis.set_major_formatter(date_format)
# 设置标签和标题
plt.xlabel('时间', fontsize=12)
plt.ylabel('秒数', fontsize=12)
plt.title('时间-响应时长趋势图', fontsize=14)

# 优化显示
plt.grid(True, linestyle='--', alpha=0.7)
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
In [ ]: