[Python] 纯文本查看 复制代码
"""
import openpyxl
from openpyxl import Workbook, load_workbook
from openpyxl.chart import (AreaChart, #AreaChart面积图表(2D区域图)
AreaChart3D, #AreaChart面积图表(3D区域图)
BarChart, #BarChart条形图/柱形图/成交量图
BubbleChart, #BubbleChart气泡图
LineChart, #LineChart折线图
LineChart3D, #LineChart3D折线图
ScatterChart, #ScatterChart散点图
PieChart, #PieChart饼图
PieChart3D, #PieChart3D饼图3D
ProjectedPieChart, #ProjectedPieChart投影饼图
StockChart, #StockChart股票图
Reference, #用来对单元范围进行标准化引用
Series) #用来方便创建图表数据系列
from copy import deepcopy #深层复制
from openpyxl.chart.axis import DateAxis #时间轴
from openpyxl.chart.series import DataPoint #
from openpyxl.chart.layout import (Layout, #布局
ManualLayout #手动布局
)
wb = Workbook()
wb_filename = r'openpyxl_chart_demos_case1.xlsx'
ws_tbbj = wb.create_sheet('图表布局', 0)
rows = [
['Size', 'Batch 1', 'Batch 2'],
[2, 40, 30],
[3, 40, 25],
[4, 50, 30],
[5, 30, 25],
[6, 25, 35],
[7, 20, 40]
]
for row in rows:
ws_tbbj.append(row)
ch1 = ScatterChart()
xvalues = Reference(ws_tbbj, min_col=1, min_row=2, max_row=7)
for i in range(2,4):
values = Reference(ws_tbbj, min_col=i, min_row=1, max_row=7)
series = Series(values, xvalues, title_from_data=True)
ch1.series.append(series)
#散点图表1
ch1.title = 'Default layout'
ch1.style = 13
ch1.x_axis.title = 'Size'
ch1.y_axis.title = 'Percentage'
ch1.legend.position = 'r' #设置图例的位置:r,l,t,b,tr,分别代表:右、左、顶、底、右上。
ws_tbbj.add_chart(ch1, 'B10')
wb.save(wb_filename)