用python绘制饼图
时间: 2023-11-18 21:05:33 浏览: 30
以下是用Python绘制饼图的示例代码:
```python
import matplotlib.pyplot as plt
# 数据
labels = ['A', 'B', 'C', 'D']
sizes = [15, 30, 45, 10]
# 绘图
fig, ax = plt.subplots()
ax.pie(sizes, labels=labels, autopct='%1.1f%%', shadow=True, startangle=90)
ax.axis('equal') # 保证饼图为正圆形
# 显示图形
plt.show()
```
运行代码后,将生成一个简单的饼图,其中A、B、C、D分别对应四个扇形区域,它们的大小分别为15%、30%、45%、10%。在代码中,我们使用了Matplotlib库的`pie()`函数来绘制饼图,其中`labels`参数指定每个扇形区域的标签,`sizes`参数指定每个扇形区域的大小(可以是百分比或绝对大小),`autopct`参数指定每个扇形区域的百分比格式,`shadow`参数指定是否显示阴影效果,`startangle`参数指定起始角度(默认为0度,即从3点钟位置开始)。最后,我们使用`axis('equal')`函数保证饼图为正圆形,然后调用`show()`函数显示图形。
相关问题
在给定数据集下用Python绘制饼图代码
好的,以下是一个用Python绘制饼图的例子:
```python
import matplotlib.pyplot as plt
# 数据
labels = ['A', 'B', 'C', 'D']
sizes = [15, 30, 45, 10]
# 饼图
fig1, ax1 = plt.subplots()
ax1.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)
ax1.axis('equal') # 等比例显示
# 显示图形
plt.show()
```
这个例子中,我们使用`matplotlib`库绘制了一个简单的饼图。首先定义了数据的标签和大小,然后使用`ax1.pie()`函数来生成饼图。`autopct`参数用来控制百分数的显示方式,`startangle`参数用来控制饼图的起始角度。最后使用`plt.show()`函数来显示图形。
用python绘制饼图字体大小的代码
你可以使用 `matplotlib` 库来绘制饼图,可以通过设置 `textprops` 参数来设置饼图中文字的字体大小。以下是一个简单的示例代码:
```python
import matplotlib.pyplot as plt
# 饼图数据
labels = ['A', 'B', 'C', 'D']
sizes = [15, 30, 45, 10]
# 设置字体大小
textprops = {'fontsize': 14}
# 绘制饼图
plt.pie(sizes, labels=labels, autopct='%1.1f%%', textprops=textprops)
# 显示图形
plt.show()
```
在上面的代码中,`textprops` 参数是一个字典,将字体大小设置为 14。你可以根据需要调整字体大小和其他属性。