Python图案特定区域着色:填充方块间隙为黄色并输出坐标
问题说明
我用Python生成包含方块的网格图案,已完成红色(水平间隙)和绿色(垂直间隙)的填充,现在需要把方块之间交叉的空白区域填充为黄色,并输出这些黄色方块的坐标。以下是我的实现代码和当前效果:
import matplotlib.pyplot as plt # Define the grid size and the number of squares grid_size = 4 square_size = 1 # Size of each square gap = 0.1 # Gap between squares # Create a new figure plt.figure(figsize=(6, 6), dpi=80) # Lists to store the coordinates of the rectangles green_rectangles = [] red_rectangles = [] # Add yellow rectangles to fill the gaps before drawing the squares for row in range(grid_size): for col in range(grid_size): # Calculate the coordinates for the bottom-left corner of the gap x = col * (square_size + gap) y = row * (square_size + gap) # Create a yellow rectangle for the gap if row < grid_size - 1: plt.Rectangle((x, y + square_size), square_size, gap, edgecolor='none', facecolor='yellow') if col < grid_size - 1: plt.Rectangle((x + square_size, y), gap, square_size + gap, edgecolor='none', facecolor='yellow') # Loop through rows and columns to draw the squares for row in range(grid_size): for col in range(grid_size): # Calculate the coordinates for the bottom-left corner of the square x = col * (square_size + gap) y = row * (square_size + gap) # Create a square using plt.Rectangle rectangle = plt.Rectangle((x, y), square_size, square_size, edgecolor='black', facecolor='black') plt.gca().add_patch(rectangle) # Add green rectangles to fill the vertical gaps for row in range(grid_size): for col in range(grid_size - 1): # Exclude the last column x = (col + 1) * (square_size + gap) - gap y = row * (square_size + gap) green_rectangle = plt.Rectangle((x, y), gap, square_size, edgecolor='none', facecolor='green') plt.gca().add_patch(green_rectangle) green_rectangles.append((x, y)) # Add red rectangles to fill the horizontal gaps for row in range(grid_size - 1): # Exclude the last row for col in range(grid_size): x = col * (square_size + gap) y = (row + 1) * (square_size + gap) - gap red_rectangle = plt.Rectangle((x, y), square_size, gap, edgecolor='none', facecolor='red') plt.gca().add_patch(red_rectangle) red_rectangles.append((x, y)) # Print the coordinates of the red and green rectangles print("Green rectangles:") for coord in green_rectangles: print(coord) print("\nRed rectangles:") for coord in red_rectangles: print(coord) # Set the aspect of the plot to be equal, so squares appear as squares plt.gca().set_aspect('equal') # Set limits to accommodate the gaps plt.xlim(0, grid_size * (square_size + gap)) plt.ylim(0, grid_size * (square_size + gap)) # Remove axis values plt.gca().set_xticks([]) plt.gca().set_yticks([]) # Add grid lines for better visualization plt.grid(True, which='both', linestyle='--', linewidth=0.5) # Show the plot plt.show()
当前效果:
修正方案
原代码中黄色方块未显示的核心问题是未将创建的矩形添加到画布,同时位置和尺寸计算错误。以下是修正后的完整代码:
import matplotlib.pyplot as plt # Define the grid size and the number of squares grid_size = 4 square_size = 1 # Size of each square gap = 0.1 # Gap between squares # Create a new figure plt.figure(figsize=(6, 6), dpi=80) # Lists to store the coordinates of the rectangles green_rectangles = [] red_rectangles = [] yellow_rectangles = [] # 新增存储黄色方块坐标的列表 # 先绘制黄色交叉间隙(方块之间的空白区域) for row in range(grid_size - 1): for col in range(grid_size - 1): # 计算黄色方块的左下角坐标:水平间隙和垂直间隙的交叉点 x = (col + 1) * (square_size + gap) - gap y = (row + 1) * (square_size + gap) - gap # 创建gap×gap的黄色正方形 yellow_rect = plt.Rectangle((x, y), gap, gap, edgecolor='none', facecolor='yellow') plt.gca().add_patch(yellow_rect) yellow_rectangles.append((x, y)) # Loop through rows and columns to draw the squares for row in range(grid_size): for col in range(grid_size): # Calculate the coordinates for the bottom-left corner of the square x = col * (square_size + gap) y = row * (square_size + gap) # Create a square using plt.Rectangle rectangle = plt.Rectangle((x, y), square_size, square_size, edgecolor='black', facecolor='black') plt.gca().add_patch(rectangle) # Add green rectangles to fill the vertical gaps for row in range(grid_size): for col in range(grid_size - 1): # Exclude the last column x = (col + 1) * (square_size + gap) - gap y = row * (square_size + gap) green_rectangle = plt.Rectangle((x, y), gap, square_size, edgecolor='none', facecolor='green') plt.gca().add_patch(green_rectangle) green_rectangles.append((x, y)) # Add red rectangles to fill the horizontal gaps for row in range(grid_size - 1): # Exclude the last row for col in range(grid_size): x = col * (square_size + gap) y = (row + 1) * (square_size + gap) - gap red_rectangle = plt.Rectangle((x, y), square_size, gap, edgecolor='none', facecolor='red') plt.gca().add_patch(red_rectangle) red_rectangles.append((x, y)) # Print the coordinates of all colored rectangles print("Green rectangles:") for coord in green_rectangles: print(coord) print("\nRed rectangles:") for coord in red_rectangles: print(coord) print("\nYellow rectangles:") for coord in yellow_rectangles: print(coord) # Set the aspect of the plot to be equal, so squares appear as squares plt.gca().set_aspect('equal') # Set limits to accommodate the gaps plt.xlim(0, grid_size * (square_size + gap)) plt.ylim(0, grid_size * (square_size + gap)) # Remove axis values plt.gca().set_xticks([]) plt.gca().set_yticks([]) # Add grid lines for better visualization plt.grid(True, which='both', linestyle='--', linewidth=0.5) # Show the plot plt.show()
关键改动说明
- 新增
yellow_rectangles列表:专门存储黄色方块的左下角坐标,用于后续输出 - 修正黄色方块的绘制逻辑:
- 只遍历
grid_size-1行和列,对应方块之间的交叉空白区域 - 坐标计算为水平间隙和垂直间隙的交叉点,尺寸为
gap×gap的正方形 - 调用
add_patch()将黄色矩形添加到画布,确保显示
- 只遍历
- 补充黄色坐标输出:在控制台打印黄色方块的坐标
内容的提问来源于stack exchange,提问作者user23495528
相关产品推荐
相关产品推荐

