首页 > 其他 > 详细

matplotlib学习日记(五)-各种饼状图的绘制

时间:2019-06-23 00:53:16      阅读:116      评论:0      收藏:0      [点我收藏+]

(一)分裂式饼状图

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams["font.sans-serif"] = ["SimHei"]
mpl.rcParams["axes.unicode_minus"] = False
labels = ["A难度水平", "B难度水平", "C难度水平", "D难度水平"]
students = [0.35, 0.15, 0.20, 0.30]
colors = ["#377eb8", "#4daf4a", "#984ea3", "#ff7f00"]
explode = [0.1, 0.1, 0.1, 0.1]
plt.pie(students,explode=explode,labels=labels,autopct="%3.1f%%",startangle=45,shadow=True,colors=colors)
‘‘‘
explode----->饼边缘偏离半径的百分比,出现分离的原因
shadow------>阴影
‘‘‘
plt.title("选择不同难度测试试卷的学生占比")
plt.show()

技术分享图片

(二)非分裂式饼状图

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams["font.sans-serif"] = ["SimHei"]
mpl.rcParams["axes.unicode_minus"] = False
labels = ["A难度水平", "B难度水平", "C难度水平", "D难度水平"]
students = [0.35, 0.15, 0.20, 0.30]
colors = ["#377eb8", "#4daf4a", "#984ea3", "#ff7f00"]
explode = [0.1, 0.1, 0.1, 0.1]
plt.pie(students, labels=labels, autopct="%3.1f%%", startangle=45, pctdistance=0.7, labeldistance=1.2, colors=colors)
‘‘‘
pctdistance------>控制百分比数值的显示位置
labeldistance----->控制标签值得显示位置
‘‘‘
plt.title("选择不同难度测试试卷的学生占比")
plt.show()

技术分享图片

(三)内嵌环形图

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams["font.sans-serif"] = ["SimHei"]
mpl.rcParams["axes.unicode_minus"] = False
elements = ["面粉", "砂糖", "奶油", "草莓酱", "坚果"]
weight1 = [40, 15, 20, 10, 15]
weight2 = [30, 25, 15, 20, 10]

colormapList = ["#e41a1c", "#377eb8", "#4daf4a", "#984ea3", "#ff7f00"]

outer_colors = colormapList
inner_colors = colormapList

wedges1, texts1, autotexts1 = plt.pie(weight1, autopct="%3.1f%%", radius=1, pctdistance=0.85, colors = colormapList,
                                      textprops=dict(color="w"), wedgeprops=dict(width = .3, edgecolor="w"))
wedges2, texts2, autotexts2 = plt.pie(weight2, autopct="%3.1f%%", radius=0.7, pctdistance=0.75, colors = colormapList,
                                      textprops=dict(color="w"), wedgeprops=dict(width = .3, edgecolor="w"))
plt.legend(wedges1, elements, fontsize=12, title="配料表", loc="center left",bbox_to_anchor=(0.91, 0, 0.3, 1))
plt.setp(autotexts1, size=15, weight="bold")
plt.setp(autotexts2, size=15, weight="bold")
plt.setp(texts1, size=12)
plt.title("不同果酱面包配料比例表")
plt.show()

技术分享图片

 

matplotlib学习日记(五)-各种饼状图的绘制

原文:https://www.cnblogs.com/ai-bingjie/p/11071057.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!