生成在5*5平方厘米的图纸上,随机分布10个大点和20个蓝色的点,点的大小为直径为0.5cm并标注颜色名字
时间: 2024-05-26 09:15:29 浏览: 10
好的,这个可以用 Python 中的 Matplotlib 库完成。以下是代码实现:
```python
import matplotlib.pyplot as plt
import numpy as np
# 创建一个 5*5 平方厘米的图纸
fig, ax = plt.subplots(figsize=(5, 5))
# 随机生成 10 个大点的坐标
big_dots = np.random.rand(10, 2)
# 随机生成 20 个蓝色点的坐标
blue_dots = np.random.rand(20, 2)
# 绘制大点
ax.scatter(big_dots[:, 0], big_dots[:, 1], s=0.5, c='red')
# 绘制蓝色点
ax.scatter(blue_dots[:, 0], blue_dots[:, 1], s=0.5, c='blue')
# 给大点加上标注
for i, dot in enumerate(big_dots):
ax.annotate(f'Big Dot {i}', xy=dot, xytext=(dot[0]-0.1, dot[1]+0.1), color='red')
# 给蓝色点加上标注
for i, dot in enumerate(blue_dots):
ax.annotate(f'Blue Dot {i}', xy=dot, xytext=(dot[0]+0.05, dot[1]+0.05), color='blue')
# 设置 x 轴和 y 轴的范围
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
# 设置 x 轴和 y 轴的标签
ax.set_xlabel('X')
ax.set_ylabel('Y')
# 显示图像
plt.show()
```
运行上述代码,可以得到如下图所示的随机分布的 10 个大点和 20 个蓝色的点,并且每个点都标注了颜色和编号:
![image-20211102180041808](https://i.loli.net/2021/11/02/6XrYQ5fRdJnExU4.png)