• 技术文章 >Python技术 >Python基础教程

    Python实用之openpyxl坐标轴范围和对数缩放

    2020-10-27 17:15:31原创5261
    在使用openpyxl时,坐标轴的调整就难住了小编。经过一番资料搜索,不光解决了这个问题还找到了对数缩放的方法,接下来就让我们一起看看吧~

    坐标轴最小和值

    为了在图表上显示特定区域,可以手动设置坐标轴的最小值和值。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    <p style="line-height: 1.75em;"><span style="font-family: 微软雅黑, "Microsoft YaHei";">    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'

        chart1.legend = None

         

        chart2 = ScatterChart()

        chart2.title = "Clipped Axes"

        chart2.x_axis.title = 'x'

        chart2.y_axis.title = '1/x'

        chart2.legend = None

         

        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("minmax.xlsx")<br></span></p>

    在某些情况下,如上面代码所示,设置坐标轴范围实际上等同于显示数据的子范围。对于大型数据集,使用Excel或者Open/Libre Office来绘制散点图(可能还有其他)时,选择数据子集方式要比设置坐标轴范围的速度更快。

    对数缩放


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

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    <p style="line-height: 1.75em;"><span style="font-family: 微软雅黑, "Microsoft YaHei";">    from openpyxl import Workbook

        from openpyxl.chart import (

            ScatterChart,

            Reference,

            Series,

        )

        import math

         

        wb = Workbook()

        ws = wb.active

         

        ws.append(['X', 'Gaussian'])

        for i, x in enumerate(range(-10, 11)):

            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

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

        ws.add_chart(chart3, "C15")

        ws.add_chart(chart4, "I15")

        ws.add_chart(chart5, "F30")

         

        wb.save("log.xlsx")<br></span></p>

    这将生成五个类似的图表:

    五张图使用了相同的数据。其中,第一个图未缩放,第二和三张图分别缩放了X和Y轴,第四张图XY轴均进行了缩放,对数基数设置为10;最后的图表XY轴均进行了缩放,但对数的底设置为e。

    轴线方向

    坐标轴可以正常显示,也可以反向显示

    轴方向由orientation属性控制,minMax表示正向,maxMin表示反向。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    <p style="line-height: 1.75em;"><span style="font-family: 微软雅黑, "Microsoft YaHei";">    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, "J1")

        ws.add_chart(chart3, "D15")

        ws.add_chart(chart4, "J15")

         

        wb.save("orientation.xlsx")<br></span></p>

    这将生成四个图表,其中每个可能的方向组合的轴如下所示:

    小伙伴们可以根据自己的需求,生成不同的图表~如需了解更多python实用知识,点击进入PyThon学习网教学中心

    专题推荐:openpyxl坐标轴范围和对数缩放;python
    上一篇:怎么用python输出和输入文件及信息? 下一篇:Python中openpyxl怎样改变字体和颜色

    相关文章推荐

    • 在pycharm中用不了openpyxl怎么办?• 做表繁杂?Python中openpyxl的用处• Python实用之安装openpyxl• Python导入openpyxl报错问题• openpyxl怎样按行和按列读取excel• openpyxl如何写入excel表格• Python之openpyxl插入折线图方法• openpyxl如何在sheet中读取、写入数据• 关于Python中openpyxl使用iter_rows()的方法• 如何解决运行较大excel文件openpyxl变慢问题• Python基础:使用 openpyxl为Excel设置行高或者列宽• Python教程:巧用openpyxl为指定区域设置边框为粗匣框线

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网