编辑代码

import matplotlib.pyplot as plt
import numpy as np

# Define the positions of the rooms and halls as 2D arrays
main = np.array([0, 0])
library = np.array([-1, 0])
kitchen = np.array([0, -1])
bedroom = np.array([1, 0])
bathroom = np.array([0, 1])
study = np.array([-1, -1])
goal = np.array([1, -1])
closet = np.array([2, 0])
balcony = np.array([0, 2])

# Plot the rooms and halls as dots with labels
plt.plot(main[0], main[1], 'bo', label='Main')
plt.plot(library[0], library[1], 'go', label='Library')
plt.plot(kitchen[0], kitchen[1], 'ro', label='Kitchen')
plt.plot(bedroom[0], bedroom[1], 'yo', label='Bedroom')
plt.plot(bathroom[0], bathroom[1], 'co', label='Bathroom')
plt.plot(study[0], study[1], 'mo', label='Study')
plt.plot(goal[0], goal[1], 'ko', label='Goal')
plt.plot(closet[0], closet[1], 'wo', label='Closet')
plt.plot(balcony[0], balcony[1], 'o', color='#FFA500', label='Balcony')

# Connect the rooms and halls with lines
plt.plot([main[0], library[0]], [main[1], library[1]], 'b-')
plt.plot([main[0], kitchen[0]], [main[1], kitchen[1]], 'b-')
plt.plot([main[0], bedroom[0]], [main[1], bedroom[1]], 'b-')
plt.plot([main[0], bathroom[0]], [main[1], bathroom[1]], 'b-')
plt.plot([library[0], study[0]], [library[1], study[1]], 'g-')
plt.plot([kitchen[0], goal[0]], [kitchen[1], goal[1]], 'r-')
plt.plot([bedroom[0], closet[0]], [bedroom[1], closet[1]], 'y-')
plt.plot([bathroom[0], balcony[0]], [bathroom[1], balcony[1]], 'c-')

# Set the axis labels and title
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Pyplot plot of the game map')

# Show the legend and the plot
plt.legend()
plt.show()