<jupyter_start><jupyter_text>Matplotlib图鉴——基础饼图 公众号：可视化图鉴<jupyter_code>import matplotlib
print(matplotlib.__version__) #查看Matplotlib版本
import pandas as pd
print(pd.__version__) #查看pandas版本
import numpy as np
print(np.__version__) #查看numpy版本
import matplotlib.pyplot as plt 
plt.rcParams['font.sans-serif'] = ['STHeiti']  #设置中文<jupyter_output>3.3.2
1.2.0
1.19.2<jupyter_text>注意，代码在以下环境全部通过测试:- Python 3.7.1- Matplotlib == 3.3.2- pandas == 1.2.0- numpy == 1.19.2因版本不同，可能会有部分语法差异，如有报错，请先检查拼写及版本是否一致！  基础饼图<jupyter_code>#-*- coding: utf-8 -*-
import matplotlib.pyplot as plt

plt.figure(figsize=(9,8),dpi = 100) 
sizes = [150,250,300,60] 
patches,text= plt.pie(sizes)
plt.title("基础饼图",fontsize = 20)

plt.show()<jupyter_output><empty_output><jupyter_text>上图为最基本的Matplotlib饼图绘制，有关plt.pie其他参数说明如下：>matplotlib.pyplot.pie(x, explode=None, labels=None, colors=None, autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=None, radius=None, counterclock=True, wedgeprops=None, textprops=None, center=(0, 0), frame=False, rotatelabels=False, *, data=None)- x: array, 代表了每部分的值。每个标签占多大，会自动去算百分比。- explode: list, 将某一块分割出来；例如[0,0,0.5,0] 将第四块分割出来且数值越大分割出来越大- label: list, 每一部分标签- colors: array-like, 使用颜色。详见色卡rgb- autopct: 百分比精确到小数点末尾- pctdistance: 数值距圆心半径倍数距离- shadow: 饼图阴影的大小- labeldistance: 标签的距离- startangle: 第一块楔形开始的角度- radius: 饼图的半径- counterlock: 画图方向- wedgeprops: 传递给楔形对象的楔形对象的参数字典。- textprops: 传递给文本对象的参数字典。- center: 图表的中心位置- frame: True则图表绘制轴框架- rotatelabels: True则每个标签旋转到相应切片的角度Returns:- patches : list, patches饼图的返回值- texts : list, 饼图外label的文本- autotexts : list, 饼图内部的文本返回值下面我们将稍微添加/修改部分参数，制作其他样式的基本饼图  基础饼图 - 修改颜色和增加标签、数值<jupyter_code>#-*- coding: utf-8 -*-
import matplotlib.pyplot as plt

plt.figure(figsize=(8,9),dpi = 100) 
labels = ['A','B','C','D'] 
sizes = [150,250,300,60] 
colors = ['#B0C4DE','#ADD8E6','#87CEFA','#4682B4'] #每块颜色定义
patches,l_text,p_text = plt.pie(sizes,
                      labels=labels,
                      colors=colors,
                      autopct = '%3.2f%%', #数值保留固定小数位
                      startangle = 90, #逆时针起始角度设置
                      pctdistance = 0.5       
                      ) #数值距圆心半径倍数距离

# 修改标签内数值和字体的大小
for t in l_text:
    t.set_size(30)
for t in p_text:
    t.set_size(17)

plt.title("基础饼图 - 修改颜色和增加标签、数值",fontsize = 25) 
plt.axis('equal')
plt.show()<jupyter_output><empty_output><jupyter_text>基础饼图 - 修改增加阴影、分割块和图例<jupyter_code>#-*- coding: utf-8 -*-
import matplotlib.pyplot as plt

plt.figure(figsize=(8,9),dpi = 100) 
labels = ['A','B','C','D'] 
sizes = [150,250,300,60] 
colors = ['#FE4365','#FC9D9A','#F9CDAD','#C8C8A9']
explode = (0,0,0.1,0) #分割出来第三部分
patches,l_text,p_text = plt.pie(sizes,
                      explode=explode,
                      labels=labels,
                      colors=colors,
                      autopct = '%3.2f%%', 
                      shadow = True, #设置阴影设置
                      startangle = 90,
                      pctdistance = 0.5       
                      )

plt.legend(patches, labels,
          loc="center left",
          bbox_to_anchor=(1, 0.2, 1, 1),
          fontsize=20)

for t in l_text:
    t.set_size(30)
for t in p_text:
    t.set_size(17)

plt.title("基础饼图 - 修改增加阴影、分割块和图例",fontsize = 25) 
plt.axis('equal')

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14907210222087008, 'fraction_numerical': 0.043808944326133255, 'mean_word_length': 3.3957219251336896, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '386810', 'license': 'MIT', 'n_tokens_mistral': 1705, 'n_tokens_neox': 1745, 'n_words': 215, 'path': 'C-饼图/基础饼图MA_C_01/MA_C_01.ipynb', 'repo_name': 'liuhuanshuo/zaoqi-data', 'size': '3287'}