用python生成环形饼图,显示不同区域中的房源数量
时间: 2024-03-04 09:50:56 浏览: 16
生成环形饼图可以使用Matplotlib库中的 `pie()` 函数。下面是一个示例代码,展示不同区域中的房源数量:
```python
import matplotlib.pyplot as plt
# 区域名称和房源数量
labels = ['东城', '西城', '朝阳', '海淀', '其他']
sizes = [100, 80, 120, 150, 50]
# 环形饼图的内外径
radii = [0.6, 1.0]
# 颜色
colors = ['#ff9999', '#66b3ff', '#99ff99', '#ffcc99', '#ebebeb']
# 绘制饼图
fig, ax = plt.subplots()
ax.pie(sizes, radius=1, colors=colors, labels=labels, autopct='%1.1f%%', startangle=90, counterclock=False)
ax.pie([1], radius=0.6, colors=['w'])
# 添加标题
ax.set_title('不同区域中的房源数量')
# 显示图像
plt.show()
```
这段代码会生成一个环形饼图,其中 `labels` 和 `sizes` 分别表示区域名称和房源数量,`radii` 表示内外径,`colors` 表示颜色,`autopct` 表示显示百分比,`startangle` 表示起始角度,`counterclock` 表示是否逆时针绘制。最后再绘制一个白色的小圆,就可以将饼图变成环形饼图了。
相关问题
python可视化嵌套环形饼图并显示百分比
以下是使用Python和matplotlib库创建嵌套环形饼图并显示百分比的示例代码:
```python
import matplotlib.pyplot as plt
# 数据
sizes = [15, 30, 45, 10]
labels = ['A', 'B', 'C', 'D']
colors = ['#ff9999','#66b3ff','#99ff99','#ffcc99']
# 创建环形饼图
fig, ax = plt.subplots()
ax.axis('equal')
inner_circle = plt.Circle((0,0), 0.5, color='white')
ax.add_artist(inner_circle)
wedges, texts, autotexts = ax.pie(sizes, colors=colors, labels=labels, autopct='%1.1f%%', startangle=90)
# 设置字体大小
plt.setp(texts, size=14)
plt.setp(autotexts, size=12, weight='bold')
# 显示图形
plt.show()
```
在此示例中,我们使用了四个不同大小的部分来创建嵌套环形饼图,并为每个部分分配了标签和颜色。我们还使用`autopct`参数来显示每个部分的百分比,并使用`setp`函数设置文本的字体大小。最后,我们使用`plt.show()`函数显示图形。
python如何让饼图不仅显示百分比还显示数量
可以使用 matplotlib 库中的 pie 函数,设置参数 autopct='%1.1f%%',即可同时显示百分比和数量。具体代码如下:
import matplotlib.pyplot as plt
labels = ['A', 'B', 'C', 'D']
sizes = [15, 30, 45, 10]
plt.pie(sizes, labels=labels, autopct='%1.1f%%')
plt.axis('equal')
plt.show()