失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > python使用matplotlib制作精美的饼图

python使用matplotlib制作精美的饼图

时间:2020-07-29 16:27:16

相关推荐

python使用matplotlib制作精美的饼图

目录

成品代码详细的分析字体大小设置设置标签值颜色设置突出显示设置绘图区第一个子图的绘制设置刻度位置美化最后记得

成品

首先上最终成品

代码

图1的展示

# 重新设置字体大小proptease = fm.FontProperties()proptease.set_size('xx-large')# font size include: ‘xx-small’,x-small’,'small’,'medium’,‘ large’,‘x-large’,‘xx-large’ or number, e.g. '12'labels = 'internal operator user', 'external user', 'external quick view collection user', 'external stripping collaborator', 'internal robot user'sizes = [62, 4048, 88, 36, 168]colors = cm.GnBu(np.arange(len(sizes)) / len(sizes)) # colormaps: Paired, autumn, rainbow, gray,spring,Darksexplode = (0, 0.2, 0, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')fig, axes = plt.subplots(figsize=(10, 4), ncols=3) # 设置绘图区域大小ax1, ax2, ax3 = axes.ravel()plt.subplot(1, 3, 1)# plt.pie(sizes, explode=explode, labels=labels, colors=colors,# autopct='%1.1f%%', shadow=False, startangle=90)patches, texts = ax1.pie(sizes, explode=explode, colors=colors,shadow=False, startangle=90)# plt.setp(autotexts, fontproperties=proptease)# Set aspect ratio to be equal so that pie is drawn as a circle.plt.axis('equal')ax3.axis('off')ax3.legend(patches, labels, loc='center left')plt.tight_layout()sizes = [60, 4402, 92, 172, 0] # -2,354,4,136,-168 = 324324 = A-F = 4726 - 4402 A外 = 4402 F外 = 4048plt.subplot(1, 3, 2)patches, texts= ax2.pie(sizes, explode=explode, colors=colors,shadow=False, startangle=90)# plt.setp(autotexts, fontproperties=proptease)# plt.setp(texts, fontproperties=proptease)# Set aspect ratio to be equal so that pie is drawn as a circle.plt.axis('equal')plt.show()

图2的展示

# -02-22 用户品类labels = 'Field discrete', 'Study', 'Fashin', 'Childrem', 'Sports', 'TV series', 'Living', 'Funny', 'Finance', 'News', 'Cars', 'Military', 'Parent-child', 'Documentory', 'Health', 'Gourment Food', \'Animals', 'Agriculture', 'Anime', 'Dance', 'Movies', 'Digital', 'Encyclopedia', 'Unboxing', 'Music', 'Variety Show', 'Stars', 'Game', 'Tourism'sizes = [28, 8, 0, 432, 12, 156, 200, 232, 0, 72, 28, 28, 28, 0, 12, 212, 48, 48, 200, 12, 420, 16, 188, 680, 16, 48,12, 1668, 0]colors = cm.GnBu(np.arange(len(sizes)) / len(sizes)) # colormaps: Paired, autumn, rainbow, gray,spring,Darksexplode = (0, 0, 0, 0.2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.2, 0, 0, 0, 0,0) # only "explode" the 2nd slice (i.e. 'Hogs')fig, axes = plt.subplots(figsize=(10, 4), ncols=3) # 设置绘图区域大小ax1, ax2, ax3 = axes.ravel()plt.subplot(1, 3, 1)patches, autotexts = ax1.pie(sizes, explode=explode, colors=colors,shadow=False, startangle=90)# Set aspect ratio to be equal so that pie is drawn as a circle.plt.axis('equal')ax3.axis('off')ax3.legend(patches, labels, loc='center left')plt.tight_layout()sizes = [24, 4, 0, 540, 12, 164, 148, 284, 0, 76, 32, 32, 28, 4, 12, 236, 52, 48, 200, 12, 468, 12, 232, 756, 20, 52,12, 1712, 4] # 372 儿童540-432 = 108 开箱756-680 = 765176 - 4804 = 372plt.subplot(1, 3, 2)patches, autotexts = ax2.pie(sizes, explode=explode, colors=colors, shadow=False, startangle=90)# Set aspect ratio to be equal so that pie is drawn as a circle.plt.axis('equal')plt.show()

详细的分析

首先声明一下,图中的百分比标注是我后期用visio加的,不是代码完成的。

在运行代码之前,需要先导入库,如果需要显示中文需要调整设置:

import matplotlib.pyplot as pltimport pandas as pdimport numpy as npfrom matplotlib import font_manager as fmfrom matplotlib import cmplt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签

字体大小设置

proptease = fm.FontProperties()proptease.set_size('xx-large')

设置这部分内容可以调整在图上显示的文字大小,举例来说,我在画图时设置autopct='%1.1f%%',自动显示饼图中的百分比,百分比数字的大小就可以通过上面这行代码来设置。

可以设置的尺寸包括:(xx-small, x-small, small, medium, large, x-large, xx-large, larger, smaller)

设置标签值

labels = 'internal operator user', 'external user', 'external quick view collection user', 'external stripping collaborator', 'internal robot user'sizes = [62, 4048, 88, 36, 168]

将你想绘制的饼图的标签和数值输入进去

颜色设置

colors = cm.GnBu(np.arange(len(sizes)) / len(sizes))

这部分设置了饼图每个模块的颜色,需要注意的是,并不需要对每一个元素都设置颜色,你甚至可以加入两个颜色,让这两个颜色循环使用。

我在这里使用的方法是cm库,cm库的渐变效果相当nice,我为了美观,设置了每个元素给定一个颜色,方法就是上面的代码。除了我使用的蓝色渐变,你还可以使用:

具体的使用可以参考其他的博客:cm库的使用方法

突出显示

explode = (0, 0.2, 0, 0, 0)

explode用于突出显示饼图,从图中分离出去的部分就是通过这个代码设置的,将第二个设置为0.2,表示第二个label中的元素分离出去,这个值越大,表示分离的程度越高

设置绘图区

fig, axes = plt.subplots(figsize=(10, 4), ncols=3) # 设置绘图区域大小ax1, ax2, ax3 = axes.ravel()

这部分设置了绘图区,因为我这里包含了三张图,所以设置了上面的参数

第一个子图的绘制

plt.subplot(1, 3, 1)# plt.pie(sizes, explode=explode, labels=labels, colors=colors,# autopct='%1.1f%%', shadow=False, startangle=90)patches, texts = ax1.pie(sizes, explode=explode, colors=colors,shadow=False, startangle=90)# plt.setp(autotexts, fontproperties=proptease)

没什么好说的,subplot绘制多个子图,设置为一行三个

pie中的参数:sizes表示传入的参数,explode表示是否有突出显示,colors表示颜色,shadow表示是否绘制成立体图,有阴影显示(设置为true并不好看 😦,startangle表示开始的第一个元素开始的位置)

这里假设我把其他注释取消,把绘制图的代码注释,就可以实现显示自动显示百分比了,但是不建议使用,因为很难调整大小,美观程度大打折扣

设置刻度

plt.axis('equal')ax3.axis('off')

第一行作用是x,y轴刻度等长,在饼图中输入这个选项,可以让圆更像一个圆

第二行作用是关闭坐标轴

关于axis的详细用法,参考/shuaishuaidefeizhu/p/14034415.html

位置美化

ax3.legend(patches, labels, loc='center left')plt.tight_layout()

legend主要用来设置图例相关的内容,其中loc用来表示图例的具体位置,他的可选的参数可以是字符,也可以是数字,默认情况下是0,这里选择的是'center left',详细的用法参考:/weixin_43412878/article/details/109561021

tight_layout会自动调整子图参数,使之填充整个图像区域。本质上就是更简单的美化整个图片

最后记得

plt.show()

这篇文章参考了很多/p/26812779的内容。

😃 作图愉快

如果觉得《python使用matplotlib制作精美的饼图》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。