热线电话:13121318867

登录
首页精彩阅读Python 操作 Excel 神器 xlsxwriter 绘图实战!
Python 操作 Excel 神器 xlsxwriter 绘图实战!
2021-12-31
收藏

作者:潮汐

来源:Python 技术

上一篇文章分享了的基本用法,但是在绘制数据的过程中难免需要给数据制作图表,根据各种数据需求绘制出不同的图表,今天的文章主要讲解 python xlsxwriter 画图模块 Chart 类的用法实例分析。

Chart 类是 XlsxWriter 模块中图表组件的基类,支持的图表类型包括面积、条形图、柱形图、折线图、饼图、散点图、股票和雷达等。

一个图表对象是通过 Workbook(工作簿)的 add_chart() 方法创建,通过 {type,'图表类型'}字典参数指定图表的类型,语句如下:

chart = workbook.add_chart({type, 'column'}) #创建一个 column(柱形)图表

然后再通过 Worksheet(工作表)的insert_chart()方法插入到指定位置,语句如下:

worksheet.insert_chart('A5', chart) #在A5单元格插入图表

图表类型

Chart 类是 XlsxWriter 模块中图表组件的基类,支持的图表类型包括面积、条形图、柱形图、折线图、饼图、散点图、股票和雷达等

  • area:面积图表;
  • bar:条形图表;
  • column:柱形图表;
  • line:线条图表;
  • pie:饼图图表;
  • scatter:散点图表;
  • stock:股票图表;
  • radar:雷达图表

XlsxWriter 绘制柱状图

使用 XlsxWriter 绘制柱状图详细代码如下:

import xlsxwriter def chart_column(): workbook = xlsxwriter.Workbook('chart.xlsx')
    worksheet = workbook.add_worksheet() # Create a new Chart object. chart = workbook.add_chart({'type': 'column'}) # Write some data to add to plot on the chart. data = [
    [13, 25, 9, 14, 35],
    [12, 14, 16, 18, 20],
    [13, 16, 19, 22, 25],
    ]

    worksheet.write_column('A1', data[0]) # 按列插入 worksheet.write_column('B1', data[1])
    worksheet.write_column('C1', data[2]) # Configure the chart. In simplest case we add one or more data series. chart.add_series({'values': '=Sheet1!$A$1:$A$5'})
    chart.add_series({'values': '=Sheet1!$B$1:$B$5'})
    chart.add_series({'values': '=Sheet1!$C$1:$C$5'}) # Insert the chart into the worksheet. worksheet.insert_chart('A7', chart)

    workbook.close() if __name__ == '__main__':
    chart_column()

绘制图片输出结果如下:

Python 操作 Excel 神器 xlsxwriter 绘图实战

XlsxWriter 绘制柱状图叠图

XlsxWriter 绘制柱状图叠图详细方法详见如下代码:

def charts_column1():

    workbook = xlsxwriter.Workbook("expense02.xlsx") # 添加一个Sheet页,不添写名字,默认为Sheet1 worksheet = workbook.add_worksheet() # 准备数据 headings = ["姓名", "语文", "数学"]
    data = [["张三", 78, 60], ["李四", 98, 89], ["王五", 88, 100]] # 样式 head_style = workbook.add_format({"bold": True, "bg_color": "yellow", "align": "center", "font": 13}) # 写数据 worksheet.write_row("A1", headings, head_style) for i in range(0, len(data)):
        worksheet.write_row("A{}".format(i + 2), data[i]) # 添加柱状图叠图子类型 chart2 = workbook.add_chart({"type": "column", "subtype": "stacked"})
    chart2.add_series({ "name": "=Sheet1!$B$1", "categories": "=Sheet1!$A$2:$a$4", "values": "=Sheet1!$B$2:$B$4" })
    chart2.add_series({ "name": "=Sheet1!$C$1", "categories": "=Sheet1!$A$2:$a$4", "values": "=Sheet1!$C$2:$C$4" })
    chart2.set_title({"name": "叠图子类型"})
    chart2.set_x_axis({"name": "姓名"})
    chart2.set_y_axis({"name": "成绩"})
    chart2.set_style(12) # 插入图表 worksheet.insert_chart("B7", chart2) # 关闭EXCEL文件 workbook.close()

绘制结果如下:

Python 操作 Excel 神器 xlsxwriter 绘图实战

XlsxWriter 绘制饼图

XlsxWriter 绘制饼图只需要新建一个工作表后添加饼图样式即可,详细绘制方式如下:

def chart_pie():
    workbook = xlsxwriter.Workbook("expense03.xlsx") # 添加一个Sheet页,不添写名字,默认为Sheet1 worksheet = workbook.add_worksheet() # 准备数据 headings = ["姓名", "语文", "数学"]
    data = [["张三", 78, 60], ["李四", 98, 89], ["王五", 88, 100]] # 样式 head_style = workbook.add_format({"bold": True, "bg_color": "yellow", "align": "center", "font": 13}) # 写数据 worksheet.write_row("A1", headings, head_style) for i in range(0, len(data)):
        worksheet.write_row("A{}".format(i + 2), data[i]) # 添加饼图 chart3 = workbook.add_chart({"type": "pie"})
    chart3.add_series({ # "name":"饼形图", "categories": "=Sheet1!$A$2:$A$4", "values": "=Sheet1!$B$2:$B$4", # 定义各饼块的颜色 "points": [
            {"fill": {"color": "yellow"}},
            {"fill": {"color": "blue"}},
            {"fill": {"color": "red"}}
        ]
    })
    chart3.set_title({"name": "饼图成绩单"})
    chart3.set_style(3) # 插入图表 worksheet.insert_chart("B7", chart3) # 关闭EXCEL文件 workbook.close()

绘制图片结果如下:

Python 操作 Excel 神器 xlsxwriter 绘图实战

总结

今天的文章讲解了神器 xlsxwriter 操作 Excel 绘制柱状图、饼图、柱状图叠图等图形详细过程,当然还有更多关于 xlsxwriter 有趣的知识点还未分享,感兴趣的朋友们可以试试,希望对大家有所帮助。

数据分析咨询请扫描二维码

最新资讯
更多
客服在线
立即咨询