Python-openpyxl教程10 - 图表布局,填充图案以及进阶图表

前文:
Python-openpyxl教程4 - 优化模式
Python-openpyxl教程5 - 与Pandas交互
Python-openpyxl教程6 - 图表之面积图和条形图
Python-openpyxl教程7 - 图表之散点图,饼图和环形图
Python-openpyxl教程8 - 图表之雷达图,股价图和曲面图图
Python-openpyxl教程9 - 轴使用之轴限制和比例

更改绘图区和图例的布局

可以通过使用布局类实例的layout属性来设置画布中图表的布局。

图表布局

大小和位置

图表可以放置在其容器中。xy调整位置,wh调整大小。单位是容器的比例。图表不能放置在其容器之外,并且宽度和高度是主要限制:如果 x + w > 1 , 则 x = 1 - w。

x是从左到右的水平位置
y是从顶部开始的垂直位置
h是图表相对于其容器的高度
w是盒子的宽度

模式

除了大小和位置之外,相关属性的模式还可以设置为factoredge,默认值是factor

layout.xMode = edge

目标

可以将layoutTarget设置为outerinner。默认值为outer

layout.layoutTarget = inner

图例布局

图例的位置可以通过设置它的位置进行控制r, l, t, btr分别控制右, 左, 顶部,底部。默认值为r

legend.position = 'tr'

或应用手动布局

legend.layout = ManualLayout()
from openpyxl import Workbook, load_workbook
from openpyxl.chart import ScatterChart, Series, Reference
from openpyxl.chart.layout import Layout, ManualLayout
from copy import deepcopy

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)

chart1 = ScatterChart()
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 = Series(values, xvalues, title_from_data=True)
    chart1.series.append(series)

chart1.title = 'Default layout'
chart1.style = 13
chart1.x_axis.title = 'Size'
chart1.y_axis.title = 'Percentage'
chart1.legend.position = 'r'

ws.add_chart(chart1, 'B10')

# 一般大小的图表,右下角
chart2 = deepcopy(chart1)
chart2.title = 'Manual chart layout'
chart2.legend.position = 'tr'
chart2.layout = Layout(manualLayout=ManualLayout(x=0.25, y=0.25, h=0.5, w=0.5))
ws.add_chart(chart2, 'J10')

# 一半大小的图表,居中
chart3 = deepcopy(chart1)
chart3.layout = Layout(manualLayout=ManualLayout(x=0.25, y=0.25, h=0.5, w=0.5, xMode='edge', yMode='edge'))
chart3.title = 'Manual chart layout, edge mode'
ws.add_chart(chart3, 'B26')

# 手动将图例放置在左下角
chart4 = deepcopy(chart1)
chart4.title = 'Manual legend layout'
chart4.legend.layout = Layout(manualLayout=ManualLayout(yMode='edge', xMode='edge', x=0, y=0.9, h=0.1, w=0.5))
ws.add_chart(chart4, 'J26')

wb.save('SampleChartLayout.xlsx')
SampleChartLayout

样式表- 添加图案

整个数据系列和单个数据点可以通过graphicalProperties广泛地设置样式。把事情做好可能需要一些时间。

from openpyxl import Workbook
from openpyxl.chart import BarChart, Reference
from openpyxl.chart.marker import DataPoint
from openpyxl.drawing.fill import PatternFillProperties, ColorChoice

wb = Workbook()
ws = wb.active

rows = [
    ("Sample",),
    (1,),
    (2,),
    (3,),
    (2,),
    (3,),
    (3,),
    (1,),
    (2,),
]

for row in rows:
    ws.append(row)

chart = BarChart()
data = Reference(ws, min_col=1, min_row=1, max_row=8)
chart.add_data(data, titles_from_data=True)
chart.title = 'Chart with patterns'

# 为一整个系列设置图案填充
series = chart.series[0]
fill = PatternFillProperties(prst='pct5')
fill.foreground = ColorChoice(prstClr='red')
fill.background = ColorChoice(prstClr='blue')
series.graphicalProperties.pattFill = fill

# 给第六个数据点设置一个图案填充(基于0)
pt = DataPoint(idx=5)
pt.graphicalProperties.pattFill = PatternFillProperties(prst='ltHorz')
series.dPt.append(pt)

ws.add_chart(chart, 'C1')
wb.save('SamplePattern.xlsx')
SamplePattern

高阶图表

仪表图

仪表图组合了一个饼图和一个环形图去创建一个"仪表"。第一个图表时包含四个切片的环形图,前三个切片与仪表的颜色相对应,第二个切片对应仪表的颜色。第四部分是甜甜圈的一半,不可见。

添加了包含三个切片的饼图。第一个和第三个切片是不可见的,因此第二个切片可以充当仪表上的指针。

使用数据系列中各个数据点的图形属性来完成效果。

from openpyxl import Workbook
from openpyxl.chart import PieChart, DoughnutChart, Series, Reference
from openpyxl.chart.series import DataPoint

data = [
    ['Dount', 'Pie'],
    [25, 75],
    [50, 1],
    [25, 124],
    [100]
]

# 基于网址 http://www.excel-easy.com/examples/gauge-chart.html

wb = Workbook()
ws = wb.active

for row in data:
    ws.append(row)

# 第一个图表是一个环形图
chart1 = DoughnutChart(firstSliceAng=270, holeSize=50)
chart1.title = 'Code coverage'
chart1.legend = None

ref = Reference(ws, min_col=1, min_row=2, max_row=5)
s1 = Series(ref, title_from_data=False)

slices = [DataPoint(idx=i) for i in range(4)]
slices[0].graphicalProperties.solidFill = 'FF3300'
slices[1].graphicalProperties.solidFill = 'FCF305'
slices[2].graphicalProperties.solidFill = '1FB714'
slices[3].graphicalProperties.noFill = True

s1.data_points = slices
chart1.series = [s1]

# 第二个图表是一个饼图
chart2 = PieChart(firstSliceAng=270)
chart2.legend = None

ref = Reference(ws, min_col=2, min_row=2, max_col=2, max_row=4)
s2 = Series(ref, title_from_data=False)

slices = [DataPoint(idx=i) for i in range(3)]
slices[0].graphicalProperties.noFill = True
slices[1].graphicalProperties.solidFill = '000000'
slices[2].graphicalProperties.noFill = True

s2.data_points = slices
chart2.series = [s2]

chart1 += chart2

ws.add_chart(chart1, 'D1')
wb.save('SampleGauge.xlsx')
SampleGauge

图表工作表

图表工作表是一个只包含图表的特殊工作表。图表的所有数据必须在不同的工作表上。

from openpyxl import Workbook
from openpyxl.chart import PieChart, Reference, Series

wb = Workbook()
ws = wb.active
ws2 = wb.create_sheet()

rows = [
    ['Bob', 3],
    ['Harry', 2],
    ['James', 4]
]

for row in rows:
    ws.append(row)

chart = PieChart()
labels = Reference(ws, min_col=1, min_row=1, max_row=3)
data = Reference(ws, min_col=2, min_row=1, max_row=3)
chart.series = (Series(data),)
chart.title = 'PieChart'

ws2.add_chart(chart)
wb.save('SampleDemo.xlsx')
SampleDemo

实际就是将数据和图表分开在不同的工作表中。


来源:
https://openpyxl.readthedocs.io/en/stable/charts/introduction.html#change-the-chart-layout
https://openpyxl.readthedocs.io/en/stable/charts/introduction.html#styling-charts
https://openpyxl.readthedocs.io/en/stable/charts/introduction.html#advanced-charts
https://openpyxl.readthedocs.io/en/stable/charts/introduction.html#using-chartsheets

你可能感兴趣的:(Python-openpyxl教程10 - 图表布局,填充图案以及进阶图表)