Python饼状图matplotlib

版权声明:本文为Zhang Phil原创文章,请不要转载! https://blog.csdn.net/zhangphil/article/details/88356546
import matplotlib
import matplotlib.pyplot as plt

# 中文乱码和坐标轴负号处理。
matplotlib.rc('font', family='SimHei', weight='bold')
plt.rcParams['axes.unicode_minus'] = False

#将画布设定为正方形。正圆。
plt.figure(figsize=(8, 8))

label = ['北京', '上海', '广州', '深圳', '成都']

#突出显示某一扇形。距离圆心n个半径。
explode = [0.02, 0.01, 0.01, 0, 0]

values = [9, 8, 7, 6, 5]

plt.pie(values,
        explode=explode,
        labels=label,
        autopct='%1.1f%%',
        startangle=90,
        radius=0.9,
        counterclock=False,  # 数据是顺时针?逆时针?
        wedgeprops={'linewidth': 0.4, 'edgecolor': 'red'},  # 设置饼图内外边框属性。
        textprops={'fontsize': 18, 'color': 'k'},  # 设置文本的属性值。k为黑色。
        center=(0, 0),  # 饼图的原点。
        pctdistance=0.7,  # 百分比数据标签与圆心的距离。
        labeldistance=1.2,  # 设置外层'城市'标签与圆心的距离。
        )

plt.title('一个简单饼图')  # 绘制标题

# 图例的位置。
# bbox_to_anchor前一个参数表示左右。第二个参数是上下。
# ncol图例一列显示。
plt.legend(loc='center right', bbox_to_anchor=(1.2, 0.5), ncol=1)

plt.show()

运行后输出结果如图:

猜你喜欢

转载自blog.csdn.net/zhangphil/article/details/88356546