import hypernetx as hnx import matplotlib.pyplot as plt from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas from io import BytesIO def draw_hypergraph(hyperedges): # 构建超图 H = hnx.Hypergraph(hyperedges) # 绘制超图 fig, ax = plt.subplots(figsize=(12, 8)) hnx.draw(H, ax=ax) # 将超图保存为图像 canvas = FigureCanvas(fig) buffer = BytesIO() canvas.print_png(buffer) buffer.seek(0) return buffer