Python-openpyxl教程9 - 轴使用之轴限制和比例

前文:
Python-openpyxl教程3 - 读写性能
Python-openpyxl教程4 - 优化模式
Python-openpyxl教程5 - 与Pandas交互
Python-openpyxl教程6 - 图表之面积图和条形图
Python-openpyxl教程7 - 图表之散点图,饼图和环形图
Python-openpyxl教程8 - 图表之雷达图,股价图和曲面图图


轴限制和比例

最小值和最大值

可以手动设置轴的最小值和最大值以在图表上显示特定区域。

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

wb = Workbook()
ws = wb.active

ws.append(['X', '1/X'])
for x in range(-10, 11):
    if x:
        ws.append([x, 1.0 / x])

chart1 = ScatterChart()
chart1.title = 'Full Axes'
chart1.x_axis.title = 'x'
chart1.y_axis.title = '1/x'
# legend: 图例
chart1.legend = None

chart2 = ScatterChart()
chart2.title = 'Clipped Axes'
chart2.x_axis.title = 'x'
chart2.y_axis.title = '1/x'
chart2.legend = None

# scaling:缩放比例
chart2.x_axis.scaling.min = 0
chart2.y_axis.scaling.min = 0
chart2.x_axis.scaling.max = 11
chart2.y_axis.scaling.max = 1.5

x = Reference(ws, min_col=1, min_row=2, max_row=22)
y = Reference(ws, min_col=2, min_row=2, max_row=22)
s = Series(y, xvalues=x)
chart1.append(s)
chart2.append(s)

ws.add_chart(chart1, 'C1')
ws.add_chart(chart2, 'C15')

wb.save('SampleMinMax.xlsx')
最大值和最小值
在某些情况下,例如所示的情况,设置轴限制实际等效于显示数据的子范围。对于大型数据集,在Excel和Open/Libre Office中使用数据的子集而不是轴限制时,散点图(可能还有其他)的渲染会快得多

对数缩放

x轴和y轴都可以对数缩放。可以将对数的底数设置为任何有效的浮点数。如果x轴按对数比例缩放,则域中的负值将被丢弃。

import math

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

wb = Workbook()
ws = wb.active

ws.append(['x', 'Gaussian'])
for i, x in enumerate(range(-10, 11)):
    # =EXP(-(($A$2/6)^2)) => [A2]=-10 => =EXP(-(-10/6)^2) => EXP(-(100/36)) =>常数'e'的'-(100/36)'次幂
    ws.append([x, '=EXP(-(($A${row}/6)^2))'.format(row=i + 2)])

chart1 = ScatterChart()
chart1.title = 'No Scaling'
chart1.x_axis.title = 'x'
chart1.y_axis.title = 'y'
chart1.legend = None

chart2 = ScatterChart()
chart2.title = 'X Log Scale'
chart2.x_axis.title = 'x(log10)'
chart2.y_axis.title = 'y'
chart2.legend = None
# scaling : 缩放比例    logBase: 对数基
chart2.x_axis.scaling.logBase = 10

chart3 = ScatterChart()
chart3.title = 'Y Log Scale'
chart3.x_axis.title = 'x'
chart3.y_axis.title = 'y(log10)'
chart3.legend = None
chart3.y_axis.scaling.logBase = 10

chart4 = ScatterChart()
chart4.title = 'Both Log Scale'
chart4.x_axis.title = 'x(log10)'
chart4.y_axis.title = 'y(log10)'
chart4.legend = None
chart4.x_axis.scaling.logBase = 10
chart4.y_axis.scaling.logBase = 10

chart5 = ScatterChart()
chart5.title = 'Log Scale Base e'
chart5.x_axis.title = 'x(ln)'
chart5.y_axis.title = 'y(ln)'
chart5.legend = None
chart5.x_axis.scaling.logBase = math.e
chart5.y_axis.scaling.logBase = math.e

x = Reference(ws, min_col=1, min_row=2, max_row=22)
y = Reference(ws, min_col=2, min_row=2, max_row=22)
s = Series(y, xvalues=x)

chart1.append(s)
chart2.append(s)
chart3.append(s)
chart4.append(s)
chart5.append(s)

ws.add_chart(chart1, 'C1')
ws.add_chart(chart2, 'K1')
ws.add_chart(chart3, 'C17')
ws.add_chart(chart4, 'K17')
ws.add_chart(chart5, 'F34')

wb.save('SampleLog.xlsx')
对数缩放

轴方向

轴可以正常显示或者反向显示。轴方向由比例orientation属性控制,该属性的值可以minMax为方向或maxMin方向。

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

wb = Workbook()
ws = wb.active

ws['A1'] = 'Archimedean Spiral'
ws.append(['T', 'X', 'Y'])
for i, t in enumerate(range(100)):
    ws.append([t / 16.0, "=$A${row}*COS($A${row})".format(row=i + 3), '=$A${row}*SIN($A${row})'.format(row=i + 3)])

chart1 = ScatterChart()
chart1.title = 'Default Orientation'
chart1.x_axis.title = 'x'
chart1.y_axis.title = 'y'
chart1.legend = None

chart2 = ScatterChart()
chart2.title = 'Flip X'
chart2.x_axis.title = 'x'
chart2.y_axis.title = 'y'
chart2.legend = None
chart2.x_axis.scaling.orientation = 'maxMin'
chart2.y_axis.scaling.orientation = 'minMax'

chart3 = ScatterChart()
chart3.title = 'Flip Y'
chart3.x_axis.title = 'x'
chart3.y_axis.title = 'y'
chart3.legend = None
chart3.x_axis.scaling.orientation = 'minMax'
chart3.y_axis.scaling.orientation = 'maxMin'

chart4 = ScatterChart()
chart4.title = 'Flip Both'
chart4.x_axis.title = 'x'
chart4.y_axis.title = 'y'
chart4.legend = None
chart4.x_axis.scaling.orientation = 'maxMin'
chart4.y_axis.scaling.orientation = 'maxMin'

x = Reference(ws, min_col=2, min_row=2, max_row=102)
y = Reference(ws, min_col=3, min_row=2, max_row=102)
s = Series(y, xvalues=x)

chart1.append(s)
chart2.append(s)
chart3.append(s)
chart4.append(s)

ws.add_chart(chart1, 'D1')
ws.add_chart(chart2, 'L1')
ws.add_chart(chart3, 'D17')
ws.add_chart(chart4, 'L17')

wb.save('SampleLog2.xlsx')

这将产生四个图表,其中的轴在每种可能的方向组合中看起来像这样:


轴方向

Important:首次进入的时候并不是这样子,点击两次切换行和列之后才这样子显示,推测是坐标轴大小的原因。原图如下:

image.png


添加第二个轴

添加第二个轴实际上涉及创建与第一个图标共享x轴但具有单独的y轴的第二个图表

from openpyxl import Workbook
from openpyxl.chart import LineChart, BarChart, Reference

wb = Workbook()
ws = wb.active

rows = [
    ['Aliens', 2, 3, 4, 5, 6, 7],
    ['Humans', 10, 40, 50, 20, 10, 50]
]

for row in rows:
    ws.append(row)

chart1 = BarChart()
v1 = Reference(ws, min_col=1, min_row=1, max_col=7)
chart1.add_data(v1, titles_from_data=True, from_rows=True)

chart1.x_axis.title = 'Days'
chart1.y_axis.title = 'Aliens'
chart1.y_axis.majorGridlines = None
chart1.title = 'Survey results'

# 创建第二个图表
chart2 = LineChart()
v2 = Reference(ws, min_col=1, min_row=2, max_col=7)
chart2.add_data(v2, titles_from_data=True, from_rows=True)
chart2.y_axis.axId = 200
chart2.y_axis.title = 'Humans'

# 将第二个图表的y轴设置为与x轴最大交叉,从而在右侧显示该图表的y轴
chart1.y_axis.crosses = 'max'
chart1 += chart2

ws.add_chart(chart1, 'D4')
wb.save('SampleSecondary.xlsx')

这将生成组合的折线图和条形图,如下所示:


image.png

来源: https://openpyxl.readthedocs.io/en/stable/charts/limits_and_scaling.html#axis-orientation

你可能感兴趣的:(Python-openpyxl教程9 - 轴使用之轴限制和比例)