|
发表于 2023-12-6 10:07:44
|
显示全部楼层
实现代码(参考)通过循环实现图中结构:
- import matplotlib.pyplot as plt
- import matplotlib.patches as patches
- # Define the number of rectangles horizontally and vertically
- num_x = 8
- num_y = 4
- # Set up the figure
- fig, ax = plt.subplots(figsize=(15, 8))
- # Create a grid of rectangles
- for i in range(num_x):
- for j in range(num_y):
- # Create a rectangle
- rect = patches.Rectangle((i*100, j*100), 100, 100, linewidth=1, edgecolor='black', facecolor='none')
- # Add the rectangle to the plot
- ax.add_patch(rect)
- # Add the coordinate text inside the rectangle
- plt.text(i*100 + 50, j*100 + 50, f'({i*100}, {j*100})', ha='center', va='center')
- # Set the limits of the plot
- ax.set_xlim(0, num_x*100)
- ax.set_ylim(0, num_y*100)
- # Hide the axes
- plt.axis('off')
- # Show the plot
- plt.show()
复制代码 |
|