python绘制出埃伦斯坦错觉图
时间: 2023-07-06 08:30:54 浏览: 548
好的,以下是Python代码,用于绘制埃伦斯坦错觉图:
```python
import numpy as np
import matplotlib.pyplot as plt
# 定义函数,用于绘制格子
def draw_grid(ax, n, gap):
for i in range(n):
for j in range(n):
x = j * gap
y = i * gap
rect = plt.Rectangle((x, y), gap, gap, facecolor='white', edgecolor='black')
ax.add_patch(rect)
if (i+j) % 2 == 0:
ax.plot([x, x+gap], [y+gap, y], color='gray', linewidth=1)
else:
ax.plot([x, x+gap], [y+gap, y], color='white', linewidth=1)
# 定义参数
n = 8 # 格子数量
gap = 1 # 格子大小
fig, ax = plt.subplots(figsize=(6, 6))
# 绘制格子
draw_grid(ax, n, gap)
# 绘制黑色方块
ax.add_patch(plt.Rectangle((2*gap, 2*gap), 4*gap, 4*gap, facecolor='black'))
# 设置坐标轴范围和标签
ax.set_xlim(0, n*gap)
ax.set_ylim(0, n*gap)
ax.set_xticks([])
ax.set_yticks([])
# 显示图像
plt.show()
```
运行该代码,将会得到一张埃伦斯坦错觉图。
阅读全文