美文网首页
openpyxl3.0官方文档(19)—— 添加第二个轴

openpyxl3.0官方文档(19)—— 添加第二个轴

作者: Sinchard | 来源:发表于2020-07-10 13:49 被阅读0次

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

    from openpyxl import Workbook
    from openpyxl.chart import (
        LineChart,
        BarChart,
        Reference,
        Series,
    )
    
    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)
    
    c1 = BarChart()
    v1 = Reference(ws, min_col=1, min_row=1, max_col=7)
    c1.add_data(v1, titles_from_data=True, from_rows=True)
    
    c1.x_axis.title = 'Days'
    c1.y_axis.title = 'Aliens'
    c1.y_axis.majorGridlines = None
    c1.title = 'Survey results'
    
    
    # Create a second chart
    c2 = LineChart()
    v2 = Reference(ws, min_col=1, min_row=2, max_col=7)
    c2.add_data(v2, titles_from_data=True, from_rows=True)
    c2.y_axis.axId = 200
    c2.y_axis.title = "Humans"
    
    # Display y-axis of the second chart on the right by setting it to cross the x-axis at its maximum
    c1.y_axis.crosses = "max"
    c1 += c2
    
    ws.add_chart(c1, "D4")
    
    wb.save("secondary.xlsx")
    
这将生成一个组合的折线图和条形图,看起来像这样: 在这里插入图片描述

相关文章

网友评论

      本文标题:openpyxl3.0官方文档(19)—— 添加第二个轴

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