使用xlsxwriter在x轴上的分类值

时间:2016-09-14 20:59:34

标签: python xlsxwriter

我有一个需要名称作为x轴值的图。我想这是通过图表对象的set_x_axis函数完成的,但无法在文档中找到正确的密钥(http://xlsxwriter.readthedocs.io/chart.html#set_x_axis)。以下代码生成下图:

chart = workbook.add_chart({'type':'scatter'})
colLetter = alphabet[1] #alphabet is list of alphabet
for ii in range(4):
    colLetter = alphabet[ii+1]
    chart.add_series({
        'name': '=Sheet1!$%s$1'%colLetter,
        'categories': '=Sheet1!$A$2:$A$'+str(lastRowNumber),
        'values': '=Sheet1!$%s$2:$%s$6'%(colLetter, colLetter),
    })
chart.set_title({'name': 'Cityblock'})
chart.set_x_axis({'name': 'Subject'})
chart.set_y_axis({'name': 'Distance'})
chart.set_style(11)
worksheet.insert_chart('F1', chart)

enter image description here

有什么建议吗?我使用xlsxwriter和python 2.7。

1 个答案:

答案 0 :(得分:1)

只需将系列类别设置为指向您要绘制的字符串,数字或日期。

例如:

import xlsxwriter

workbook = xlsxwriter.Workbook('chart.xlsx')
worksheet = workbook.add_worksheet()

# Add a column chart.
chart = workbook.add_chart({'type': 'column'})

# Write some data to add to plot on the chart.
worksheet.write_column('A1', ['Bob', 'Eve', 'Ann'])
worksheet.write_column('B1', [5, 10, 7])

# Configure the chart.
chart.add_series({'categories': '=Sheet1!$A$1:$A$3',
                  'values':     '=Sheet1!$B$1:$B$3'})

# Insert the chart into the worksheet.
worksheet.insert_chart('D1', chart)

workbook.close()

输出:

enter image description here