不說(shuō)廢話(huà),直接上代碼和注解。 注:左右滑動(dòng)可以看完整代碼。 import matplotlib.pyplot as plt import numpy as np
def create_5g_signaling_flow(): # 創(chuàng)建圖形和坐標(biāo)軸 fig, ax = plt.subplots(figsize=(12, 8))
# 設(shè)置網(wǎng)絡(luò)實(shí)體的位置 entities = ['UE', 'gNB', 'AMF', 'SMF', 'UDM'] x_positions = np.linspace(1, 9, len(entities)) entity_positions = dict(zip(entities, x_positions))
# 繪制垂直生命線(xiàn) y_top = 10 y_bottom = 0 for entity, x in entity_positions.items(): # 繪制實(shí)體框和標(biāo)簽 ax.add_patch(plt.Rectangle((x-0.5, y_top), 1, 0.8, facecolor='lightblue', edgecolor='black')) # 在框內(nèi)居中顯示網(wǎng)元名稱(chēng) ax.text(x, y_top 0.4, entity, ha='center', va='center') # 繪制生命線(xiàn) ax.plot([x, x], [y_top, y_bottom], 'k--', alpha=0.3)
# 繪制信令消息 def draw_arrow(start_entity, end_entity, y_pos, label, step_num, style='-'): start_x = entity_positions[start_entity] end_x = entity_positions[end_entity] arrow_style = '->' if style == '-' else '->' line_style = style
ax.annotate('', xy=(end_x, y_pos), xytext=(start_x, y_pos), arrowprops=dict(arrowstyle=arrow_style, linestyle=line_style))
# 添加帶編號(hào)的標(biāo)簽 mid_x = (start_x end_x) / 2 ax.text(mid_x, y_pos 0.1, f'{step_num}. {label}', ha='center', va='bottom')
# 繪制必選流程(實(shí)線(xiàn)) step = 1 y = 9 draw_arrow('UE', 'gNB', y, 'RRC Setup Request', step) step = 1; y -= 0.8 draw_arrow('gNB', 'UE', y, 'RRC Setup', step) step = 1; y -= 0.8 draw_arrow('UE', 'AMF', y, 'Registration Request', step) step = 1; y -= 0.8 draw_arrow('AMF', 'UDM', y, 'Authentication Data Request', step) step = 1; y -= 0.8 draw_arrow('UDM', 'AMF', y, 'Authentication Data Response', step) step = 1; y -= 0.8 draw_arrow('AMF', 'UE', y, 'Authentication Request', step) step = 1; y -= 0.8 draw_arrow('UE', 'AMF', y, 'Authentication Response', step)
# 繪制可選流程(虛線(xiàn)) step = 1; y -= 0.8 draw_arrow('AMF', 'SMF', y, 'Session Establishment Request', step, '--') step = 1; y -= 0.8 draw_arrow('SMF', 'AMF', y, 'Session Establishment Accept', step, '--') step = 1; y -= 0.8 draw_arrow('AMF', 'UE', y, 'Registration Accept', step) step = 1; y -= 0.8 draw_arrow('UE', 'AMF', y, 'Registration Complete', step)
# 設(shè)置圖形屬性 ax.set_xlim(0, 10) ax.set_ylim(y_bottom - 1, y_top 2) ax.axis('off') plt.title('5G Signaling Flow')
# 保存圖片 plt.savefig('5g_signaling_flow.png', bbox_inches='tight', dpi=300) plt.close()
if __name__ == '__main__': create_5g_signaling_flow()
注解: 【這將幫助您理解,如何復(fù)用這個(gè)代碼, 舉一反三,稍微改吧改吧,畫(huà)別的流程圖。】
上述代碼執(zhí)行效果圖: 【這個(gè)圖的準(zhǔn)確性先不管它哈,今天重點(diǎn)是看怎么畫(huà)圖?!?/strong> 謝謝關(guān)注。
|
|
來(lái)自: bin仔學(xué)習(xí)園地 > 《通信類(lèi)5g》