目录

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

成品

首先上最终成品

代码

图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 = 324     324 = A-F = 4726 - 4402   A外 = 4402  F外 = 4048
plt.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的展示

# 2021-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 = 76      5176 - 4804 = 372
plt.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 plt
import pandas as pd
import numpy as np
from matplotlib import font_manager as fm
from 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并不好看

python使用matplotlib制作精美的饼图相关推荐

  1. python之matplotlib制作雷达图

    python之matplotlib制作雷达图 示例代码: import numpy as np import matplotlib.pyplot as plt import matplotlibmat ...

  2. Python利用matplotlib制作雷达扫描显示仪(之后可结合串口和超声波传感器使用,亦可以做为仪表盘使用)

    Python利用matplotlib制作雷达扫描显示仪(之后可结合串口和超声波传感器使用,亦可以做为仪表盘使用).内有制作雷达扫描GIF图的源代码包含. ''' matplotlib雷达扫描 ''' ...

  3. python removebg_不到20行实现Python代码即可制作精美证件照

    无论是我们上学时还之后的工作中,基本都需要用到电子证件照片,这类照片基本都对照片尺寸.背景色有要求,本文我们来看一下如何只用不到 20 行 Python 代码完成证件照片的制作. 简介 制作证件照我们 ...

  4. python使用matplotlib制作画板和轴

    [高心星出品] 文章目录 前言 一.安装matplotlib库 二.使用matploatlib制作画板和轴 1.引入库 2.创建画板和轴 总结 前言 Matplotlib 是 Python 的绘图库. ...

  5. python之matplotlib制作双Y轴图含详细代码解释

    前言:好久没更新啦,最近在参加OCALE全国跨境电商大赛,今天更新的内容是python制作双Y轴图片. 目录 一.函数介绍 二.实际应用 2.1 实验数据展示 2.2 代码实现: 2.3 最终结果显示 ...

  6. python电子相册制作代码大全_20 行 Python 代码即可制作精美证件照

    无论是我们上学时还之后的工作中,基本都需要用到电子证件照片,这类照片基本都对照片尺寸.背景色有要求,本文我们来看一下如何只用不到 20 行 Python 代码完成证件照片的制作. 简介 制作证件照我们 ...

  7. 基于python的证件照_不到20行实现Python代码即可制作精美证件照

    无论是我们上学时还之后的工作中,基本都需要用到电子证件照片,这类照片基本都对照片尺寸.背景色有要求,本文我们来看一下如何只用不到 20 行 Python 代码完成证件照片的制作. 简介 制作证件照我们 ...

  8. python之matplotlib制作基础图表以及图例,标注,marker,中文设置

    前言: 1. 基础折线图 2.柱状图 3.条形图 4.散点图 5.饼图 前言: 咸鱼了好久,又来更新了,最近这个月考试比较多,也在搭建自己的资料分享学习QQ,主要是分享一些学习资料和大家相互交流解决一 ...

  9. 不到 20 行 Python 代码即可制作精美证件照!不需要去图文店了!

    无论是我们上学时还之后的工作中,基本都需要用到电子证件照片,这类照片基本都对照片尺寸.背景色有要求,本文我们来看一下如何只用不到 20 行 Python 代码完成证件照片的制作. 简介 制作证件照我们 ...

最新文章

  1. 1.18.5.流式概念、动态表(Dynamic Table)、DataStream上的关系查询、动态表 连续查询(Continuous Query)、在流上定义表、处理时间
  2. rabittmq java spring_消息队列 RabbitMQ 与 Spring 整合使用的实例代码
  3. 一次性缴纳6万元,退休后每月领1500元养老金,你愿意吗?
  4. Spring Data JPA教程:简介
  5. 华为Y9 Prime 2019曝光:无刘海无水滴全面屏+升降前摄
  6. android 蓝牙与单片机通信原理图,手机蓝牙与HC-06蓝牙模块控制单片机程序加APP...
  7. sql server死锁_了解SQL Server中的死锁定义
  8. 64位win7搭建php mysql_Win7 64位操作系统下配置PHP+MySql+Apache环境
  9. IOS 多个UIImageView 加载高清大图时内存管理
  10. 计算机辅助初中英语教学,利用多媒体优化初中英语课堂教学课题研究
  11. 遥感如何穿透云雨雾和黑夜,从太空看破地球?
  12. Idea导入的项目不能运行
  13. java计算机毕业设计吉他库存管理源码+mysql数据库+系统+lw文档+部署
  14. VMware: 虚拟机启动没有IP地址
  15. Windows自带录屏
  16. 初步使用计算机教学设计,【教资笔试——科目三】信息技术教学设计范例
  17. 毕业设计选题推荐 - python毕设选题推荐 - 2023最新毕设选题 - 如何选题 避免被坑
  18. hbuildx中文乱码
  19. JS操作excel文件
  20. 假如再有三年生命,世界的教育改革家--乔布斯

热门文章

  1. UML 类图各符号含义速查
  2. 冯 • 诺依曼体系结构与操作系统
  3. Linux基础命令之echo(涉及bash命令引用及替换部分内容)
  4. Offset Explorer中添加Kafka连接
  5. js 判断文件是否存在
  6. 以管理员的身份从cmd进入数据库mysql
  7. struts2系列(二):struts2参数传递错误、struts2的输入错误验证
  8. 去水印源码,不是接口,可以支持接口,短视频去水印,源码,算法
  9. ACM 算法竞赛入门级模板 ------ (比赛技巧工具)
  10. 供应链对企业竞争有哪些优势?