环图
运行结果为:
代码如下:
import matplotlib.pyplot as plt
import pandas as pd
plt.rcParams['font.sans-serif'] = ['KaiTI'] # 设置字体为楷体
labels = ['A', 'B', 'C', 'D', '其他']
share_laptop = [0.45, 0.25, 0.15, 0.05, 0.10]
share_pc = [0.35, 0.35, 0.08, 0.07, 0.15]
colors = ['c', 'r', 'y', 'g', 'gray']
plt.figure(figsize=(8, 8)) # 设置画布大小
# 画外环:
plt.pie(share_laptop,
radius=1, # 设置半径为1
explode=[0.001, 0.001, 0.001, 0.001, 0.001], # 偏离半径的百分比
# labels=labels,# 设置标签
autopct='%.2f%%', # 设置数据精度
pctdistance=0.85, # 指定autopct的位置刻度
colors=colors, # 设置颜色开始角度
startangle=180, # 开始角度
textprops={'fontsize': 12, 'color': 'k'}, # 设置标签字体大小和颜色
wedgeprops=dict(width=0.3, edgecolor='w') # 设置饼弧宽度和边框颜色
)
# 画内环
plt.pie(share_pc,
radius=0.7, # 设置半径为1
explode=[0.001, 0.001, 0.001, 0.001, 0.001], # 偏离半径的百分比
# labels=labels,# 设置标签
autopct='%.2f%%', # 设置数据精度
pctdistance=0.8, # 指定autopct的位置刻度
colors=colors, # 设置颜色开始角度
startangle=180, # 开始角度
textprops={'fontsize': 12, 'color': 'k'}, # 设置标签字体大小和颜色
wedgeprops=dict(width=0.3, edgecolor='w') # 设置饼弧宽度和边框颜色
)
# 图例
plt.legend(
labels,
title='公司列表',
loc='center right',
bbox_to_anchor=(1.1, 0.5) # 指定图例在轴的位置
)
plt.title('2017年笔记本及PC电脑市场份额', fontsize=20)
plt.show()