Python-openpyxl教程7 - 图表之散点图,饼图和环
2021-04-07 本文已影响0人
庄周幻梦
前文:
Python-openpyxl教程1 - openpyxl简介
Python-openpyxl教程2 - 简单使用
Python-openpyxl教程3 - 读写性能
Python-openpyxl教程4 - 优化模式
Python-openpyxl教程5 - 与Pandas交互
Python-openpyxl教程6 - 图表之面积图和条形图
散点图
散点图或xy图类似于某些折线图。主要区别在于,一个系列的值相对于另一个系列被绘制。当值无序时,这很有用。
from openpyxl import Workbook
from openpyxl.chart import ScatterChart, Reference, Series, marker
wb = Workbook()
ws = wb.active
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.append(row)
chart = ScatterChart()
chart.title = 'Scatter Chart'
chart.style = 13
chart.x_axis.title = 'Size'
chart.y_axis.title = 'Percentage'
xvalues = Reference(ws, min_col=1, min_row=2, max_row=7)
for i in range(2, 4):
values = Reference(ws, min_col=i, min_row=1, max_row=7)
# Series(values, xvalues, zvalues, title, title_from_data)
series = Series(values, xvalues=xvalues, title_from_data=True)
chart.series.append(series)
series.marker = marker.Marker('dot') # 修改标记点样式。默认是没有标记点的。
series.graphicalProperties.line.noFill = True # 是否有线条。
ws.add_chart(chart, 'A10')
wb.save('SampleScatter.xlsx')
规范指出散点图有以下几种类型: "线","线标记","标记","平滑","平滑标记"。但是,至少在Microsoft Excel中,这只是其他设置的快捷方式,否则这些设置无效。为了与折线图保持一致,应手动设置每个系列的样式 |
原文代码中散点图的系列样式和折线一样,因此加上了修改系列的样式的方法。修改为其他散点样式。
marker.Marker 可选项有: 'x', 'dash', 'square', 'circle', 'auto', 'picture', 'triangle', 'diamond', 'dot', 'plus', 'star'
饼状图
饼状图
饼图将数据绘制为一个圆的切片,每个切片代表整个百分比。切片按顺时针方向绘制,圆的顶部为0°。饼图只能获取单个数据系列。图表标题将默认为系列标题。
from copy import deepcopy
from openpyxl import Workbook
from openpyxl.chart import PieChart, Reference, ProjectedPieChart
from openpyxl.chart.series import DataPoint
data = [
['pie', 'Sold'],
['Apple', 50],
['Cherry', 30],
['Pumpkin', 10],
['Chocolate', 40]
]
wb = Workbook()
ws = wb.active
for row in data:
ws.append(row)
pie = PieChart()
labels = Reference(ws, min_col=1, min_row=2, max_row=5)
data = Reference(ws, min_col=2, min_row=1, max_row=5)
pie.add_data(data, titles_from_data=True)
pie.set_categories(labels) # 设置周标签名称
pie.title = 'Pies sold by category'
# 把饼图的第一片切下来
slice = DataPoint(idx=0, explosion=20) # DataPoint: idx:某个数据的下标, 切片和饼图距离
pie.series[0].data_points = [slice] # data_points 是一个列表,里面可以存放多个DataPoint对象。
ws.add_chart(pie, 'D1')
ws = wb.create_sheet(title='Projection')
data = [
['Page', 'Views'],
['Search', 95],
['Products', 4],
['Offers', 0.5],
['Sales', 0.5]
]
for row in data:
ws.append(row)
projected_pie = ProjectedPieChart() # 创建一个投影图表对象
projected_pie.type = 'pie' # 投影类型为饼图
projected_pie.splitType = 'val' # 根据'值'(value)划分图表
labels = Reference(ws, min_col=1, min_row=2, max_row=5)
data = Reference(ws, min_col=2, min_row=1, max_row=5)
projected_pie.add_data(data, titles_from_data=True)
projected_pie.set_categories(labels)
ws.add_chart(projected_pie, 'A10')
projected_bar = deepcopy(projected_pie)
projected_bar.type = 'bar' # 投影类型为条形图
projected_bar.splitType = 'pos' # split by position
ws.add_chart(projected_bar, 'A27')
wb.save('SamplePie.xlsx')
饼图
投影饼图
投影饼图从饼图中提取一些切片,并将它们投影到第二个饼图或条形图中。当数据系列中有几个较小的项目时,这很有用。可以根据百分比,val(ue)或pos(ition)划分图表。如果为设置任何内容,则应用程序决定使用哪个。另外可以自定义拆分。
投影饼图
3D 饼图
也可以使用3D效果创建饼图。
from openpyxl import Workbook
from openpyxl.chart import PieChart3D, Reference
data = [
['Pie', 'Sold'],
['Apple', 50],
['Cherry', 30],
['Pumpkin', 10],
['Chocolate', 40]
]
wb = Workbook()
ws = wb.active
for row in data:
ws.append(row)
pie = PieChart3D()
labels = Reference(ws, min_col=1, min_row=2, max_row=5)
data = Reference(ws, min_col=2, min_row=1, max_row=5)
pie.add_data(data, titles_from_data=True)
pie.set_categories(labels)
pie.title = 'Pies sold by category'
ws.add_chart(pie, 'D1')
wb.save('SamplePie3D.xlsx')
3D饼图
渐变饼图
饼图也可以使用梯度系列创建
暂无源码,百度未找到类似代码。有知道的可以帮给出下!!
环形图
环形图类似于饼图,不同之处在于他们使用环而不是圆。他们还可以将多个系列的数据绘制为同心环。
from copy import deepcopy
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) # 引用[A2:A5]
data = Reference(ws, min_col=2, min_row=1, max_row=5) # 引用[B1:B5]
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)] # 创建四个DataPoint()对象
plain, jam, lime, chocolate = slices
chart.series[0].data_points = slices
plain.graphicalProperties.solidFill = 'FAE1D0' # 设置plain数据点的填充颜色
jam.graphicalProperties.solidFill = 'BB2244' # 设置jam数据点的填充颜色
lime.graphicalProperties.solidFill = '22DD22' # 设置lime数据点的填充颜色
chocolate.graphicalProperties.solidFill = '61210B' # 设置chocolate数据点的填充颜色
chocolate.explosion = 10 # 切出去的块的距离
ws.add_chart(chart, 'E1')
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)
chart2.series.append(series2)
ws.add_chart(chart2, 'E17')
wb.save('SampleDoughnut.xlsx')
环形图
来源: https://openpyxl.readthedocs.io/en/stable/charts/introduction.html