使用 matplotlib绘制由20个从小到大递增的环形气泡图
时间: 2024-02-06 19:10:25 浏览: 21
以下是使用 matplotlib 绘制由20个从小到大递增的环形气泡图的示例代码:
```python
import numpy as np
import matplotlib.pyplot as plt
# 生成随机数据
np.random.seed(0)
x = np.arange(20)
y = np.random.rand(20)
sizes = np.linspace(50, 200, 20)
# 绘制图形
fig, ax = plt.subplots()
for i in range(20):
circle = plt.Circle((x[i], y[i]), sizes[i]/100, color='blue', alpha=0.5)
ax.add_artist(circle)
# 设置坐标轴范围和标签
ax.set_xlim(-1, 20)
ax.set_ylim(-0.1, 1.1)
ax.set_xticks(x)
ax.set_xticklabels(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't'], fontsize=12)
ax.set_yticks([])
ax.set_xlabel('x', fontsize=12)
# 显示图形
plt.show()
```
这段代码将生成一个包含20个从小到大递增的气泡图的环形图。每个气泡都是一个圆形,大小随着数据值的递增而增大。通过添加 `Circle` 对象来创建每个气泡,并通过 `add_artist` 方法将其添加到图形中。可以使用 `xlim` 和 `ylim` 方法设置坐标轴的范围,使用 `set_xticks` 和 `set_xticklabels` 方法设置 x 轴的标签。最后,使用 `show` 方法显示图形。