X 和 Y 轴作为第 1 列和第 2 列的图表

Chart with X and Y axis as column 1 and column 2

参考下图:

使用下面的代码我得到了第一张图,但我需要的是第二张图。请让我知道我哪里错了

import openpyxl

xfile = openpyxl.load_workbook("chart.xlsx")
sheet = xfile["Chart"]
c = openpyxl.chart.LineChart()

c.title = "Graph"
c.style = 1

c.y_axis.title = 'Time'
c.x_axis.title = 'WF'

data = openpyxl.chart.Reference(sheet, min_col=1, min_row=1, max_col=2, max_row=sheet.max_row)
c.add_data(data, titles_from_data=True)

sheet.add_chart(c, "C1")
xfile.save("chart.xlsx")

你的问题不是很清楚。 我想你的问题只是关于 X 轴,而不是关于图表的样式。

要获得 "same" 图表,您必须设置 X 轴的标签 select 使用第一列中的那些值。

    import openpyxl

    chart_directory = "chart.xlsx"

    xfile = openpyxl.load_workbook(chart_directory)
    sheet = xfile["Chart"]
    c = openpyxl.chart.LineChart()

    c.title = "Graph"
    c.style = 2

    c.y_axis.title = 'Time'
    c.x_axis.title = 'WF'

    #Select the lables to set
    labels = openpyxl.chart.Reference(sheet, min_col=1, min_row=2, max_row=sheet.max_row, max_col=1)
    #Select the values to set
    data = openpyxl.chart.Reference(sheet, min_col=2, min_row=1, max_row=sheet.max_row)

    #Set the values
    c.add_data(data, titles_from_data=True)
    #Set the labels
    c.set_categories(labels)

    sheet.add_chart(c, "C1")
    xfile.save(chart_directory)

在此示例代码中,您可以看到我 select 从第 2 行到最后一行将标签作为第 1 列。 之后,您可以 select 从第 1 行打印为第 2 列的值(因此它保留系列的图例名称)直到最后一行。

您必须在图表中设置数据,然后是标签。