https://openpyxl.readthedocs.io/en/stable/charts/line.html

说明:来自英文手册未翻译

Line Charts

Line charts allow data to be plotted against a fixed axis. They are similar to scatter charts, the main difference is that with line charts each data series is plotted against the same values. Different kinds of axes can be used for the secondary axes.

Similar to bar charts there are three kinds of line charts: standard, stacked and percentStacked.

该部分代码如下(直到图片位置 结束):

from datetime import date

from openpyxl import Workbook
from openpyxl.chart import (
LineChart,
Reference,
)
from openpyxl.chart.axis import DateAxis

wb = Workbook()
ws = wb.active

rows = [
[‘Date’, ‘Batch 1’, ‘Batch 2’, ‘Batch 3’],
[date(2015,9, 1), 40, 30, 25],
[date(2015,9, 2), 40, 25, 30],
[date(2015,9, 3), 50, 30, 45],
[date(2015,9, 4), 30, 25, 40],
[date(2015,9, 5), 25, 35, 30],
[date(2015,9, 6), 20, 40, 35],
]

for row in rows:
ws.append(row)

c1 = LineChart()
c1.title = “Line Chart”
c1.style = 13
c1.y_axis.title = ‘Size’
c1.x_axis.title = ‘Test Number’

data = Reference(ws, min_col=2, min_row=1, max_col=4, max_row=7)
c1.add_data(data, titles_from_data=True)

# Style the lines
s1 = c1.series[0]
s1.marker.symbol = “triangle”
s1.marker.graphicalProperties.solidFill = “FF0000” # Marker filling
s1.marker.graphicalProperties.line.solidFill = “FF0000” # Marker outline

s1.graphicalProperties.line.noFill = True

s2 = c1.series[1]
s2.graphicalProperties.line.solidFill = “00AAAA”
s2.graphicalProperties.line.dashStyle = “sysDot”
s2.graphicalProperties.line.width = 100050 # width in EMUs

s2 = c1.series[2]
s2.smooth = True # Make the line smooth

ws.add_chart(c1, “A10”)

from copy import deepcopy
stacked = deepcopy(c1)
stacked.grouping = “stacked”
stacked.title = “Stacked Line Chart”
ws.add_chart(stacked, “A27”)

percent_stacked = deepcopy(c1)
percent_stacked.grouping = “percentStacked”
percent_stacked.title = “Percent Stacked Line Chart”
ws.add_chart(percent_stacked, “A44”)

# Chart with date axis
c2 = LineChart()
c2.title = “Date Axis”
c2.style = 12
c2.y_axis.title = “Size”
c2.y_axis.crossAx = 500
c2.x_axis = DateAxis(crossAx=100)
c2.x_axis.number_format = ‘d-mmm’
c2.x_axis.majorTimeUnit = “days”
c2.x_axis.title = “Date”

c2.add_data(data, titles_from_data=True)
dates = Reference(ws, min_col=1, min_row=2, max_row=7)
c2.set_categories(dates)

ws.add_chart(c2, “A61”)

wb.save(“line.xlsx”)

3D Line Charts

In 3D line charts the third axis is the same as the legend for the series.

该部分代码如下(直到图片位置 结束):

from datetime import date

from openpyxl import Workbook
from openpyxl.chart import (
LineChart3D,
Reference,
)
from openpyxl.chart.axis import DateAxis

wb = Workbook()
ws = wb.active

rows = [
[‘Date’, ‘Batch 1’, ‘Batch 2’, ‘Batch 3’],
[date(2015,9, 1), 40, 30, 25],
[date(2015,9, 2), 40, 25, 30],
[date(2015,9, 3), 50, 30, 45],
[date(2015,9, 4), 30, 25, 40],
[date(2015,9, 5), 25, 35, 30],
[date(2015,9, 6), 20, 40, 35],
]

for row in rows:
ws.append(row)

c1 = LineChart3D()
c1.title = “3D Line Chart”
c1.legend = None
c1.style = 15
c1.y_axis.title = ‘Size’
c1.x_axis.title = ‘Test Number’

data = Reference(ws, min_col=2, min_row=1, max_col=4, max_row=7)
c1.add_data(data, titles_from_data=True)

ws.add_chart(c1, “A10”)

wb.save(“line3D.xlsx”)

个人使用excel生成图标基本操作,代码示例

from openpyxl import load_workbook
from openpyxl import Workbook
from openpyxl.chart import BarChart, Reference, Series, LineChart# chart = BarChart()  # 条形 图表对象
chart = LineChart()  # 线性 图表对象
# chart.title = "压力变化记录折线图"
chart.style = 2
chart.width = 10
chart.height = 6wb = load_workbook('.\\sample.xlsx')
# ws1=wb.active
# wb = Workbook()
ws = wb.active
# for i in range(10):
#     ws.append([i])values = Reference(ws, min_col=2, min_row=1, max_col=2, max_row=10)
chart.add_data(values)values = Reference(ws, min_col=1, min_row=1, max_col=1, max_row=10)
chart.set_categories(values)ws.add_chart(chart, "E20")
# Save the file
wb.save(".\\sample.xlsx")  # e:\\sample.xlsx

python openpyxl包excel 绘制 线性图表相关推荐

  1. python openpyxl删除excel特定行数据遇到的问题

    python openpyxl删除excel特定行数据遇到的问题 for row in sheet_codereview.rows:for cell in row:print(cell.row,cel ...

  2. python3 excel 图表导出图片_使用python代码将excel中的图表导出为图片

    python导出Excel图表类 前期准备就绪,网上已有类似的导出Excel图表类,但是在后面的使用中发现问题,即关键函数已在下面代码中标红: 调用代码: 执行成功,接下来到上面设置的导出路径查看导出 ...

  3. python openpyxl 操作excel ,使用 conditional_formatting,insert_rows

    场景 python 使用 openpyxl 操作 excel 对于简单的格式来说还是挺方便的,但是对于复杂的格式,操作没有手动来的简单,如果对于复杂的格式也不是用python来做的.对于一个原有的报表 ...

  4. Python openpyxl 之 Excel 文档简单操作

    背景:生活中常常因日常工作,在记录统计方面需频繁处理较多 Excel 表格,这部分工作虽可由人工完成,但这样会显得有些繁琐且可能存在偏差,遂闲时查阅了是否有相关基于python处理Excel表格的学习 ...

  5. Python openpyxl打开有公式的excel表取值错误的解决办法,Python openpyxl获取excel有公式的单元格的数值错误,Python操作excel(.xlsx)封装类

    Python openpyxl打开有公式的表格,如果直接读取,会出现有公式的单元格为空或零的情况. 参见: https://blog.csdn.net/weixin_45903952/article/ ...

  6. python openpyxl 操作excel xlrd.biffh.XLRDError: Excel xlsx file; not supported错误修改

    python xlrd/openpyxl 操作excel xlrd.biffh.XLRDError: Excel xlsx file; not supported错误修改 代码: 错误: excel读 ...

  7. python openpyxl 操作 Excel

    安装openpyxl pip install openpyxl 工作簿的新建打开和保存 1,读取和保存xlsx文件(创建工作簿) import openpyxl as vb file = r'周杰伦. ...

  8. [转载] 在Python中使用Matplotlib绘制常见图表

    参考链接: Python | 使用openpyxl模块在Excel工作表中绘制图表 3 Matplotlib 是一个非常强大的 Python 画图工具,通过绘制线图.散点图.等高线图.条形图.柱状图. ...

  9. Python.openpyxl操作Excel

    Python 操作 Excel 存取数据 1. openpyxl 模块 1.1 安装 openpyxl 模块 1.2 Excel 文件的三个对象 1.2.1 Workbook 对象 1.2.2 Wor ...

最新文章

  1. c# 第28节 面向对象概述
  2. springmvc笔记(1)—使用maven快速构建springmvc项目
  3. ASP.NET基础教程-DataGrid表格控件-模板列的使用
  4. ASP.NET MVC 3 Internationalization
  5. find linux 指定后缀_linux下find(文件查找)命令的用法总结
  6. Centos7 安装gitlab 8.7.5
  7. python实现手机号归属地相关信息查询
  8. Android异步处理:Handler+Looper+MessageQueue深入详解
  9. ajax写入txt,javascript结合ajax读取txt文件内容
  10. 质量管理 六西格玛-黑带大师
  11. 国内第一款企业集中管理平台--极通EWEBS3.0
  12. Large-scale Video Classification with Convolutional Neural Networks
  13. 安装配置Axis2,为Eclipse安装配置Axis2插件教程
  14. conver Json to map by fastJson
  15. javaScript中值传递和引用传递
  16. 微信公众号开发获取微信用户openid及访问url身份认证方式
  17. java女程序员的出路,持续更新~
  18. 如何用计算机蓝牙发送文件,怎么用蓝牙传文件
  19. 搜索引擎关键词优化可以怎么做?
  20. C++的内存机制,程序变成exe执行的过程

热门文章

  1. 通勤程序员必备降噪蓝牙耳机,高续航低延迟静享HIFI音乐
  2. 【Linux系统】第一篇:基础指令篇
  3. SimpleDateFormat /String/Date互转
  4. 小程序语音红包中遇到的 语音识别silk转wav格式 如何在线转 或者mp3转wav格式...
  5. java的rsa加密_Java实现RSA加密算法
  6. “纸张尺寸”2022蓝桥杯c/c++ c组E
  7. 配置resolve映射路径
  8. 全网黑白、灰色网页变回正常彩色方法
  9. 给计算机老师的一封赞美信,赞美老师的一封信
  10. Android应用生死轮回的那些事儿(6) - 武器库完结篇