zoukankan      html  css  js  c++  java
  • Python第三方库之openpyxl(9)

    Python第三方库之openpyxl(9)

    油炸圈饼图

    甜甜圈图表与饼图相似,只是他们用的是环而不是圆。他们还可以将几个系列的数据绘制成同心环

    from openpyxl import Workbook
    
    from openpyxl.chart import (
        DoughnutChart,
        Reference,
        Series,
    )
    from openpyxl.chart.series import DataPoint
    
    data = [
        ['Pie', 2014, 2015],
        ['Plain', 40, 50],
        ['Jam', 2, 10],
        ['Lime', 20, 30],
        ['Chocolate', 30, 40],
    ]
    
    wb = Workbook()
    ws = wb.active
    
    for row in data:
        ws.append(row)
    
    chart = DoughnutChart()
    labels = Reference(ws, min_col=1, min_row=2, max_row=5)
    data = Reference(ws, min_col=2, min_row=1, max_row=5)
    chart.add_data(data, titles_from_data=True)
    chart.set_categories(labels)
    chart.title = "Doughnuts sold by category"
    chart.style = 26
    
    # Cut the first slice out of the doughnut
    slices = [DataPoint(idx=i) for i in range(4)]
    plain, jam, lime, chocolate = slices
    chart.series[0].data_points = slices
    plain.graphicalProperties.solidFill = "FAE1D0"
    jam.graphicalProperties.solidFill = "BB2244"
    lime.graphicalProperties.solidFill = "22DD22"
    chocolate.graphicalProperties.solidFill = "61210B"
    chocolate.explosion = 10
    
    ws.add_chart(chart, "E1")
    
    from copy import deepcopy
    
    chart2 = deepcopy(chart)
    chart2.title = None
    data = Reference(ws, min_col=3, min_row=1, max_row=5)
    series2 = Series(data, title_from_data=True)
    series2.data_points = slices
    chart2.series.append(series2)
    
    ws.add_chart(chart2, "E17")
    
    wb.save("doughnut.xlsx")

    运行结果

  • 相关阅读:
    JSP脚本指令
    JSP编译指令——page、include
    c++基础(三):多态
    c++基础(一):数据类型和结构
    c++基础(二):成员he派生类
    python小算法(二)
    python的内存管理
    初识java之Mina(一)
    python的小爬虫的基本写法
    python小算法(一)
  • 原文地址:https://www.cnblogs.com/Keys819/p/9315557.html
Copyright © 2011-2022 走看看