编辑代码

import matplotlib.pyplot as plt

# 初始化画布
fig, ax = plt.subplots(figsize=(10, 8))
ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
ax.set_title("《红楼梦》贾府布局图(林黛玉入府路线)", fontsize=14, fontproperties='SimHei')
ax.axis('off')  # 隐藏坐标轴

# 定义建筑坐标与标签
buildings = {
    "荣国府正门": (5, 1),
        "垂花门": (5, 3),
            "穿堂": (5, 4.5),
                "贾母院(五间大正房)": (5, 6),
                    "贾政王夫人院(东廊)": (7, 6),
                        "贾赦邢夫人院(西廊)": (3, 6),
                            "凤姐院(西角门)": (3, 5),
                                "贾母后院(宝玉住所)": (5, 8),
                                    "大观园(北侧)": (5, 9.5)
                                    }

                                    # 绘制建筑标记(此处缩进已统一为4空格)
                                    for name, (x, y) in buildings.items():
                                        ax.plot(x, y, 'ro', markersize=8)  # 建筑标记
                                            ax.text(x + 0.1, y + 0.1, name, fontsize=9, fontproperties='SimHei', va='bottom')

                                            # 绘制路线箭头
                                            path_order = ["荣国府正门", "垂花门", "穿堂", "贾母院(五间大正房)", 
                                                          "贾政王夫人院(东廊)", "贾赦邢夫人院(西廊)", "凤姐院(西角门)", "贾母后院(宝玉住所)"]
                                                          for i in range(len(path_order)-1):
                                                              start = buildings[path_order[i]]
                                                                  end = buildings[path_order[i+1]]
                                                                      ax.annotate("", xy=end, xytext=start, 
                                                                                      arrowprops=dict(arrowstyle="->", color="red", lw=1.5))

                                                                                      # 添加方位标注
                                                                                      ax.text(0.5, 5, '西', fontsize=12, rotation=90, ha='center', va='center', color='gray')
                                                                                      ax.text(9.5, 5, '东', fontsize=12, rotation=-90, ha='center', va='center', color='gray')
                                                                                      ax.text(5, 9.8, '北', fontsize=12, ha='center', va='center', color='gray')
                                                                                      ax.text(5, 0.2, '南', fontsize=12, ha='center', va='center', color='gray')

                                                                                      # 图例说明
                                                                                      ax.text(1, 9.5, '注:红色箭头为林黛玉入府路线', fontsize=10, color='red', fontproperties='SimHei')

                                                                                      plt.show()