code:import os
import sys
import matplotlib.pyplot as plt
import numpy as np
# 设置标准输出编码为utf-8以解决中文乱码
sys.stdout.reconfigure(encoding='utf-8')
def print_recent_lottery_results(file_path, num_recent=50):
try:
with open(file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()
valid_lines = [line.strip() for line in lines if line.strip()]
recent_lines = valid_lines[-num_recent:]
#print(f"最近{num_recent}期福彩3D开奖号码:\n")
results_array = [] # 存储30行9列结果的数组
for line in recent_lines:
parts = line.split()
if len(parts) >= 5:
period = parts[0]
date = parts[1]
# 提取原始号码并转换为整数
original_nums = list(map(int, parts[2:5]))
# 对号码进行升序排序
sorted_nums = sorted(original_nums)
# 调整排序后的号码:中间数+1,最大数+2
adjusted_nums = [sorted_nums[0], sorted_nums[1] + 1, sorted_nums[2] + 2]
# 计算+12和+24的号码
plus12_nums = [num + 12 for num in adjusted_nums]
plus24_nums = [num + 24 for num in adjusted_nums]
# 合并为9列数据并添加到结果数组
full_row = adjusted_nums + plus12_nums + plus24_nums
results_array.append(full_row)
# 打印号码信息
#riginal_str = ' '.join(map(str, original_nums))
#sorted_str = ' '.join(map(str, sorted_nums))
#adjusted_str = ' '.join(map(str, adjusted_nums))
#print(f"期号:{period} 日期:{date} 原始号码:{original_str} 排序后:{sorted_str} 调整后:{adjusted_str}")
# 打印30行9列数组结果
#print("\n生成的30行9列数组:")
#for i, row in enumerate(results_array, 1):
# print(f"第{i}期: {row}")
# 生成6列新数组(筛选5new_array = []
for row in results_array:
# 筛选大于5且小于30的数
filtered_nums = [num for num in row if 5 < num < 30]
# 确保恰好6列,不足则用0填充,超过则取前6个
new_row = filtered_nums[:6] + [0]*(6 - len(filtered_nums[:6]))
new_array.append(new_row)
# 打印6列新数组
#print("\n生成的30行6列新数组(仅包含5#for i, row in enumerate(new_array, 1):
# print(f"第{i}期: {row}")
# 绘制网格点阵图
plt.rcParams["font.family"] = ["SimHei", "Microsoft YaHei", "SimSun"] # 设置中文字体
plt.figure(figsize=(15, 8))
# 准备数据
points = [] # 存储每期每列的点坐标 (period, num)
x = []
y = []
for period_idx, row in enumerate(new_array):
period = period_idx + 1 # 期数从1开始
current_points = []
for num in row:
if num != 0:
current_points.append((period, num))
x.append(period)
y.append(num)
else:
current_points.append(None) # 用None表示0值
points.append(current_points)
# 绘制散点图
plt.scatter(x, y, color='blue', s=50, alpha=0.7)
# 新增:同一行内数值差=1的连线
for period_idx in range(len(points)):
current_period = points[period_idx]
for col in range(len(current_period) - 1):
p1 = current_period[col]
p2 = current_period[col + 1]
if p1 and p2 and abs(p1[1] - p2[1]) == 1:
plt.plot([p1[0], p2[0]], [p1[1], p2[1]], 'gray', linewidth=0.5)
# 原有:相邻行数值差绝对值≤1的连线
for period_idx in range(1, len(points)): # 从第二期开始
prev_points = [p for p in points[period_idx-1] if p is not None]
curr_points = [p for p in points[period_idx] if p is not None]
# 比较前一行和当前行的所有点对
for p_prev in prev_points:
for p_curr in curr_points:
if abs(p_prev[1] - p_curr[1]) <= 1:
plt.plot([p_prev[0], p_curr[0]], [p_prev[1], p_curr[1]], 'gray', linewidth=0.5)
# 设置网格
plt.grid(True, linestyle='--', alpha=0.7)
# 设置坐标轴
plt.xlabel('期数', fontsize=12)
plt.ylabel('号码', fontsize=12)
plt.title('50期开奖号码排序网格点阵图', fontsize=15)
# 设置X轴和Y轴范围
plt.xlim(0, 51)
plt.ylim(5, 30)
# 显示图形
plt.show()
except FileNotFoundError:
print(f"错误:文件 '{file_path}' 未找到。")
except Exception as e:
print(f"处理文件时出错:{str(e)}")
if __name__ == "__main__":
data_file = "3d_asc.txt"
print_recent_lottery_results(data_file)