美文网首页Python高效办公
openpyxl3.0官方文档(12)—— 散点图

openpyxl3.0官方文档(12)—— 散点图

作者: Sinchard | 来源:发表于2020-07-08 12:18 被阅读0次

    散点图或xy图类似于一些折线图。主要的区别是,一系列值与另一系列值相对应。这在值无序的情况下很有用。

        from openpyxl import Workbook
        from openpyxl.chart import (
            ScatterChart,
            Reference,
            Series,
        )
        
        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 = Series(values, xvalues, title_from_data=True)
            chart.series.append(series)
        
        ws.add_chart(chart, "A10")
        
        wb.save("scatter.xlsx")
        
    
    在这里插入图片描述

    规范中指出,散点图有以下类型:“线条”、“线条标记”、“标记”、“平滑”、“平滑标记”。但是,至少在Microsoft Excel中,这只是其他设置的快捷方式。为了与折线图保持一致,应手动设置每个系列的样式。

    相关文章

      网友评论

        本文标题:openpyxl3.0官方文档(12)—— 散点图

        本文链接:https://www.haomeiwen.com/subject/qwoqcktx.html