一、区域图

2D

面积图与线图相似,加上绘制线下方的面积。不同的变种可通过设置分组到“标准”,“叠”或“percentstacked”;“标准”是默认的。

from openpyxl import Workbookfrom openpyxl.chart import (

AreaChart,

Reference,

Series,)wb = Workbook()ws = wb.activerows = [

['Number', 'Batch 1', 'Batch 2'],

[2, 40, 30],

[3, 40, 25],

[4, 50, 30],

[5, 30, 10],

[6, 25, 5],

[7, 50, 10],]for row in rows:

ws.append(row)chart = AreaChart()chart.title = "Area Chart"chart.style = 13chart.x_axis.title = 'Test'chart.y_axis.title = 'Percentage'cats = Reference(ws, min_col=1, min_row=1, max_row=7)data = Reference(ws, min_col=2, min_row=1, max_col=3, max_row=7)chart.add_data(data, titles_from_data=True)chart.set_categories(cats)ws.add_chart(chart, "A10")wb.save("area.xlsx")

3D

您还可以创建三维区域图

from openpyxl import Workbookfrom openpyxl.chart import (

AreaChart3D,

Reference,

Series,)wb = Workbook()ws = wb.activerows = [

['Number', 'Batch 1', 'Batch 2'],

[2, 30, 40],

[3, 25, 40],

[4 ,30, 50],

[5 ,10, 30],

[6,  5, 25],

[7 ,10, 50],]for row in rows:

ws.append(row)chart = AreaChart3D()chart.title = "Area Chart"chart.style = 13chart.x_axis.title = 'Test'chart.y_axis.title = 'Percentage'chart.legend = Nonecats = Reference(ws, min_col=1, min_row=1, max_row=7)data = Reference(ws, min_col=2, min_row=1, max_col=3, max_row=7)chart.add_data(data, titles_from_data=True)chart.set_categories(cats)ws.add_chart(chart, "A10")wb.save("area3D.xlsx")

This produces a simple 3D area chart where third axis can be used to replace the legend:

二、柱状图

他以下设置会影响不同的图表类型。

通过将类型分别设置为col或bar来切换垂直和水平条形图。

当使用堆叠图表时,重叠需要设置为100。

如果条形是水平的,则x和y轴被折叠。

fromopenpyxl importWorkbook

fromopenpyxl.chart importBarChart, Series, Reference

wb =Workbook(write_only=True)

ws =wb.create_sheet()

rows =[

('Number', 'Batch 1', 'Batch 2'),

(2, 10, 30),

(3, 40, 60),

(4, 50, 70),

(5, 20, 10),

(6, 10, 40),

(7, 50, 30),

]

forrow inrows: ws.append(row)

chart1 =BarChart()

chart1.type ="col"chart1.style =10chart1.title ="Bar Chart"chart1.y_axis.title ='Test number'chart1.x_axis.title ='Sample length (mm)'data =Reference(ws, min_col=2, min_row=1, max_row=7, max_col=3)

cats =Reference(ws, min_col=1, min_row=2, max_row=7)

chart1.add_data(data, titles_from_data=True)

chart1.set_categories(cats)

chart1.shape =4ws.add_chart(chart1, "A10")

fromcopy importdeepcopy

chart2 =deepcopy(chart1)

chart2.style =11chart2.type ="bar"chart2.title ="Horizontal Bar Chart"ws.add_chart(chart2, "G10")

chart3 =deepcopy(chart1)

chart3.type ="col"chart3.style =12chart3.grouping ="stacked"chart3.overlap =100chart3.title ='Stacked Chart'ws.add_chart(chart3, "A27")

chart4 =deepcopy(chart1)

chart4.type ="bar"chart4.style =13chart4.grouping ="percentStacked"chart4.overlap =100chart4.title ='Percent Stacked Chart'ws.add_chart(chart4, "G27")

wb.save("bar.xlsx")

这将产生四个图表,说明各种可能性。

3D条形图

您还可以创建三维条形图。

from openpyxl import Workbookfrom openpyxl.chart import (

Reference,

Series,

BarChart3D,)

wb = Workbook()ws = wb.active

rows = [

(None, 2013, 2014),

("Apples", 5, 4),

("Oranges", 6, 2),

("Pears", 8, 3)]

for row in rows:

ws.append(row)

data = Reference(ws, min_col=2, min_row=1, max_col=3, max_row=4)

titles = Reference(ws, min_col=1, min_row=2, max_row=4)

chart = BarChart3D()

chart.title = "3D Bar Chart"

chart.add_data(data=data, titles_from_data=True)

chart.set_categories(titles)

ws.add_chart(chart, "E5")

wb.save("bar3d.xlsx")

这会生成一个简单的3D条形图。

气泡图

气泡图类似于散点图,但使用第三维来确定气泡的大小。图表可以包括多个系列。

from openpyxl import Workbook

from openpyxl.chart import Series, Reference, BubbleChart

wb = Workbook()

ws = wb.active

rows = [

("Number of Products", "Sales in USD", "Market share"),

(14, 12200, 15),

(20, 60000, 33),

(18, 24400, 10),

(22, 32000, 42),

(),

(12, 8200, 18),

(15, 50000, 30),

(19, 22400, 15),

(25, 25000, 50),

]

for row in rows:

ws.append(row)

chart = BubbleChart()

chart.style = 18 # use a preset style

# add the first series of data

xvalues = Reference(ws, min_col=1, min_row=2, max_row=5)

yvalues = Reference(ws, min_col=2, min_row=2, max_row=5)

size = Reference(ws, min_col=3, min_row=2, max_row=5)

series = Series(values=yvalues, xvalues=xvalues, zvalues=size, title="2013")

chart.series.append(series)

# add the second

xvalues = Reference(ws, min_col=1, min_row=7, max_row=10)

yvalues = Reference(ws, min_col=2, min_row=7, max_row=10)

size = Reference(ws, min_col=3, min_row=7, max_row=10)

series = Series(values=yvalues, xvalues=xvalues, zvalues=size, title="2014")

chart.series.append(series)

# place the chart starting in cell E1

ws.add_chart(chart, "E1")

wb.save("bubble.xlsx")

三、线图

折线图允许相对于固定轴绘制数据。 它们与散点图类似,主要区别在于使用折线图,每个数据系列都绘制相同的值。 辅助轴可以使用不同种类的轴。

与条形图类似,有三种折线图:标准,堆叠和百分比重叠。

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线图

在3D线图中,第三轴与该系列的图例相同。

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

剩下的略

打赏

微信扫一扫,打赏作者吧~

python 英语翻译 excel_翻译篇Day2-用python读写Excel–数据图表篇相关推荐

  1. python pandas excel数据处理_Python处理Excel数据-pandas篇

    Python处理Excel数据-pandas篇 非常适用于大量数据的拼接.清洗.筛选及分析 在计算机编程中,pandas是Python编程语言的用于数据操纵和分析的软件库.特别是,它提供操纵数值表格和 ...

  2. Python读写Excel数据(指定某行某列)

    Python读写Excel数据(指定某行某列) 在Python数据处理中,经常需要对Excel表格进行读写操作,本文的代码介绍了如何通过行与列的下标进行数据的读写:代码对数据格式有要求,读数据要求文件 ...

  3. Python海龟数据分析,第七次全国人口普查历年数据图表.py

    """Python海龟数据分析,第七次全国人口普查历年数据图表.py使用Python的海龟模块能进行数据分析,显示统计图标,画柱状图吗?答案是肯定的.下面的程序主要使用了 ...

  4. Java解析xml文件dom4j篇(基于xml配置文件完成Excel数据的导入、导出功能完整实现)

    DOM4J解析XML文件 dom4j是一个Java的XML API,是jdom的升级产品,用来读写XML文件.另外对比其他API读写XML文件,dom4j是一个十分优秀的JavaXML API,具有性 ...

  5. python快速整理excel_使用pandas包用python清理excel数据

    我用pandas使用pd.read_excel将xls文件读入 Python 我正在努力清理我的数据,但我已经离开了我的联盟. 每条记录之间都有一个空行.在示例pic中,它优于第4,9和11行. 有一 ...

  6. python 标准库 excel_超全整理|Python 操作 Excel 库 xlwings 常用操作详解!

    大家好,我是早起. 在之前的文章中我们曾详细的讲解了如何使用openpyxl 操作Excel,其实在Python中还有其他可以直接操作 Excel 文件的库,如 xlwings.xlrd.xlwt 等 ...

  7. 数据分析用Python的Pandas模块读写Excel数据及一些实用的变换、排序、筛选、清洗、列拆分、表合并及相关的坑

    为了学习Pandas的用法,在网上查了很多帖子,但大部分帖子都是搬运工,不是太简单就是太复杂,不直观,似乎得是行家才行,而且没有讲到有实际意义的时候,就已经断片儿了,在这里分享一些完全是自己的数字+亲 ...

  8. python数据比对怎么做_同事给你迷之Excel数据,4个数据处理案例教你Python数据对比更新...

    经常听别人说 Python 在数据领域有多厉害,结果学了很长时间,连数据处理都麻烦得要死.后来才发现,原来不是 Python 数据处理厉害,而是他有数据分析神器-- pandas 前言 有时候我们需要 ...

  9. python读写excel数据--pandas

    文章目录 1读写excel数据 1.1 读: 1.1 写: 2举例 2.1 要求 2.2 实现 1读写excel数据 利用pandas可以很方便的读写excel数据 1.1 读: data_in = ...

最新文章

  1. python文件操作举例
  2. 通信专业学python有用吗-通信算法工程师需要学python吗
  3. 开发者和程序员需要关注的42个播客
  4. systemctl添加开机启动
  5. day05python
  6. 机器学习(part2)--线性方程组的列表达
  7. SAP Fiori应用里的get org sales target
  8. SpringBoot 2.x 集成Redis
  9. Visual Studio的语法着色终于调得赏心悦目
  10. 苹果发布 iOS、macOS 更新,系统修复英特尔重大漏洞
  11. python画热力图
  12. 配置linux danted socks服务
  13. 华为服务器 修改为传统模式,电脑设置成服务器模式
  14. ROS 摄像头校准与Apriltag标签使用
  15. VMware 虚拟机操作命令收集
  16. 高级程序员和普通程序员有哪些区别?
  17. Mongo和Couch对比
  18. C++之观察者(Event-Subscriber)模式
  19. 深度学习与人脸识别系列(4)__利用caffe训练深度学习模型
  20. 程序人生 - 鼠标手是什么?如何应对鼠标手!

热门文章

  1. ce修改服务器的数据库,数据库服务器的调优步骤
  2. 交换机putty怎么调试_弱电工程视频监控系统设计、安装、调试、维护全过程讲解...
  3. 电商产品页多种出彩表现设计手法!
  4. 室内高品质海报框架模型模板(Photoshop PSD)
  5. android menu item 显示,Android 如何通过menu id来得到menu item 控件 .
  6. cad和python哪个好学_cad制图工资一般多少 就业前景好不好
  7. mysql 堵塞_Mysql解决USE DB堵塞详解
  8. hadoop ubantu环境搭建_Ubuntu16.04 下 hadoop的安装与配置(伪分布式环境)
  9. Python--多线程学习(11.3)
  10. 利用ptrace和memfd_create混淆程序名和参数