编辑代码

import matplotlib.pyplot as plt
from datetime import datetime, timedelta

# 现有数据
times = [
    '2025/04/29 22:00',
    '2025/04/30 03:00',
    '2025/04/30 11:06',
    '2025/04/30 12:34',
    '2025/04/30 16:30',
    '2025/04/30 23:50',
    '2025/05/01 16:15',
    '2025/05/01 20:43',
    '2025/05/01 22:01',
    '2025/05/02 11:36',
    '2025/05/03 11:30',
    '2025/05/03 14:11'
]
levels = [128, 130, 133, 134, 136, 138, 140, 141, 142, 147, 152, 153]

# 解析现有时间
times = [datetime.strptime(t, '%Y/%m/%d %H:%M') for t in times]

# 计算平均每级时间
total_seconds = 0
for i in range(1, len(times)):
    diff = times[i] - times[i - 1]
    total_seconds += diff.total_seconds()
total_hours = total_seconds / 3600
total_levels = levels[-1] - levels[0]
average_time_per_level = total_hours / total_levels

# 绘制真实趋势图(现有数据)
plt.figure(figsize=(10, 6))
plt.plot(times, levels, marker='o', color='blue', label='Actual Data')
plt.xlabel('Time')
plt.ylabel('Level')
plt.title('Actual Level Trend')
plt.xticks(rotation=45)
plt.tight_layout()
plt.legend()
plt.savefig('actual_trend.png')  # 保存真实趋势图
plt.close()

# 预测到200级
current_level = 153
current_time = times[-1]
predicted_times_200 = [current_time]
predicted_levels_200 = [current_level]

target_level_200 = 200
while current_level < target_level_200:
    current_time += timedelta(hours=average_time_per_level)
    current_level += 1
    predicted_times_200.append(current_time)
    predicted_levels_200.append(current_level)

# 绘制200级趋势图
plt.figure(figsize=(10, 6))
plt.plot(times + predicted_times_200, levels + predicted_levels_200, marker='o', color='orange', label='Actual + Predicted (to 200)')
plt.xlabel('Time')
plt.ylabel('Level')
plt.title('Level Trend to 200')
plt.xticks(rotation=45)
plt.tight_layout()
plt.legend()
plt.savefig('trend_to_200.png')  # 保存到200级的趋势图
plt.close()

# 预测到300级
current_level = 153
current_time = times[-1]
predicted_times_300 = [current_time]
predicted_levels_300 = [current_level]

target_level_300 = 300
while current_level < target_level_300:
    current_time += timedelta(hours=average_time_per_level)
    current_level += 1
    predicted_times_300.append(current_time)
    predicted_levels_300.append(current_level)

# 绘制300级趋势图
plt.figure(figsize=(10, 6))
plt.plot(times + predicted_times_300, levels + predicted_levels_300, marker='green', label='Actual + Predicted (to 300)')
plt.xlabel('Time')
plt.ylabel('Level')
plt.title('Level Trend to 300')
plt.xticks(rotation=45)
plt.tight_layout()
plt.legend()
plt.savefig('trend_to_300.png')  # 保存到300级的趋势图
plt.close()

print("三张趋势图已分别保存为 actual_trend.png、trend_to_200.png、trend_to_300.png")