openpyxl3.0官方文档(14)—— 甜甜圈图

甜甜圈图表与饼图类似,只是它们使用了一个环而不是一个圆,还可以绘制出若干系列的数据作为中心环。

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

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值