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")
    
在这里插入图片描述

你可能感兴趣的:(openpyxl3.0官方文档(14)—— 甜甜圈图)