用Python绘制xy轴均为-5到5的225个均匀的点的代码,每个点和相邻的点连接虚线
时间: 2024-06-10 16:05:54 浏览: 132
好的,下面是用Python绘制xy轴均为-5到5的225个均匀的点的代码,每个点和相邻的点连接虚线的实现:
```
import matplotlib.pyplot as plt
# 设置坐标轴的范围
x = y = range(-5, 6)
# 生成所有点的坐标
points = [(a, b) for a in x for b in y]
# 绘制散点图
plt.scatter(*zip(*points), s=10)
# 绘制虚线
for i in range(len(x)):
for j in range(len(y)):
if i < len(x) - 1: # 绘制横向虚线
plt.plot([points[i * len(x) + j][0], points[(i + 1) * len(x) + j][0]],
[points[i * len(x) + j][1], points[(i + 1) * len(x) + j][1]],
'k--', linewidth=0.5)
if j < len(y) - 1: # 绘制纵向虚线
plt.plot([points[i * len(x) + j][0], points[i * len(x) + j + 1][0]],
[points[i * len(x) + j][1], points[i * len(x) + j + 1][1]],
'k--', linewidth=0.5)
# 设置坐标轴标签和标题
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('225 Uniformly Distributed Points with Dashed Connecting Lines')
# 显示图像
plt.show()
```
执行以上代码,即可绘制出如下图像:
![225\_uniformly\_distributed\_points.png](https://img-blog.csdn.net/20180513221304947?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ljcGx1cw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/q/75)
阅读全文