• python 用 matplotlib 绘制圆环形嵌套饼图步骤详解


    1、加载库

    import matplotlib as mpl
    import matplotlib.pyplot as plt

    2、单层圆环饼图

    # 配置字体,显示中文
    mpl.rcParams['font.sans-serif'] = ['SimHei']  
    # 配置坐标轴刻度值模式,显示负号
    mpl.rcParams['axes.unicode_minus'] = False
    
    # 定义数据
    elements = ['面粉', '砂糖', '牛奶', '草莓酱', '坚果']
    weight1 = [40, 15, 20, 10, 15]
    cs = ['red', 'orange', 'yellow', 'green', 'cyan']
    
    # 对数据进行排序
    x = list(zip(elements, weight1, cs))
    x.sort(key=lambda e: e[1], reverse=True)
    [elements, weight1, cs] = list(zip(*x))
    
    outer_cs = cs
    inner_cs = cs
    
    
    # 初始化图表区
    fig = plt.figure(figsize=(12, 8),
                     facecolor='cornsilk'
                    )
    
    # 绘制外层圆环
    wedges1, texts1, autotexts1 = plt.pie(x=weight1,
                                          autopct='%3.1f%%',
                                          radius=1,
                                          pctdistance=0.85,
                                          startangle=90,
                                          counterclock=False,
                                          colors=outer_cs,
                                          # 锲形块边界属性字典
                                          wedgeprops={'edgecolor': 'white',
                                                      'linewidth': 1,
                                                      'linestyle': '-'
                                                      },
                                          # 锲形块标签文本和数据标注文本的字体属性
                                          textprops=dict(color='k',  #  字体颜色
                                                         fontsize=14,
                                                         family='Arial'               
                                                         )
                                         )
    
    # 绘制中心空白区域
    plt.pie(x=[1],
            radius=0.6,
            colors=[fig.get_facecolor()]
           )
    
    # 设置图例
    plt.legend(handles=wedges1,
               loc='best',
               labels=elements,
               title='配料表',
               facecolor = fig.get_facecolor(),    # 图例框的填充颜色
               edgecolor='darkgray',
               fontsize=12        
              )
    
    plt.title(s='果酱面包的配料表占比',
              color='blue',
              size=15,
              weight='bold'
             );

    图形:

     

    3、内嵌圆环形饼图

    # 配置字体,显示中文
    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]
    cs = ['red', 'orange', 'yellow', 'green', 'cyan']
    
    # 对数据进行排序
    x = list(zip(elements, weight1, weight2, cs))
    x.sort(key=lambda e: e[1], reverse=True)
    [elements, weight1, weight2, cs] = list(zip(*x))
    
    # 初始化图表区
    fig = plt.figure(figsize=(12, 8),
                     facecolor='cornsilk'
                    )
    
    # 绘制外层圆环
    wedges1, texts1, autotexts1 = plt.pie(x=weight1,
                                          autopct='%3.1f%%',
                                          radius=1,
                                          pctdistance=0.85,
                                          startangle=90,
                                          counterclock=False,
                                          colors=cs,
                                          # 锲形块边界属性字典
                                          wedgeprops={'edgecolor': 'white',
                                                      'linewidth': 1,
                                                      'linestyle': '-'
                                                      },
                                          # 锲形块标签文本和数据标注文本的字体属性
                                          textprops=dict(color='k',  #  字体颜色
                                                         fontsize=14,
                                                         family='Arial'               
                                                         )
                                         )
    
    # 绘制内层圆环
    wedges2, texts2, autotexts2 = plt.pie(x=weight2,
                                          autopct='%3.1f%%',
                                          radius=0.7,
                                          pctdistance=0.75,
                                          startangle=90,
                                          counterclock=False,
                                          colors=inner_cs,
                                          # 锲形块边界属性字典
                                          wedgeprops={'edgecolor': 'white',
                                                      'linewidth': 1,
                                                      'linestyle': '-'
                                                      },
                                          # 锲形块标签文本和数据标注文本的字体属性
                                          textprops=dict(color='k',  #  字体颜色
                                                         fontsize=14,
                                                         family='Arial'
                                                         )
                                         )
    
    # 绘制中心空白区域
    plt.pie(x=[1],
            radius=0.4,
            colors=[fig.get_facecolor()]
           )
    
    # 设置图例
    plt.legend(handles=wedges1,
               loc='best',
               labels=elements,
               title='配料表',
               facecolor = fig.get_facecolor(),    # 图例框的填充颜色
               edgecolor='darkgray',
               fontsize=12        
              )
    
    plt.title(s='不同果酱面包的配料表比较',
              color='blue',
              size=15,
              weight='bold'
             );

    图形:

     

  • 相关阅读:
    css实现鼠标移入table时出现滚动条且table内容不移位
    SASS学习笔记
    html5--canvas学习笔记
    在iframe里调用parent.func()引出的js函数运行在它们被定义的作用域里,而不是它们被执行的作用域里
    css background-position:x% y%
    js十进制等互相转换
    jsp中的动作元素:<jsp:plugin>
    终于有时间写代码了
    OK 开始实践书上的项目一:即使标记
    我没有忘记敲代码
  • 原文地址:https://www.cnblogs.com/shanger/p/13074100.html
Copyright © 2020-2023  润新知