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")

    运行结果

  • 相关阅读:
    kubuntu设置
    odoo git环境搭建
    ubuntu Gnome 14.10添加打印机
    ubuntu 14.10安装Balsamiq Mockups
    elementary os luna安装配置
    OpenERP QWeb模板标签笔记
    pycharm3 注册码
    统计项目代码
    odoo filter 日期
    opencart 安装
  • 原文地址:https://www.cnblogs.com/Keys819/p/9315557.html
Copyright © 2011-2022 走看看