search

Home  >  Q&A  >  body text

excel - python xlsxwriter画图表时出错?

我想用xlsxwriter画一个带数据标记的折线图,就像下图这样的:

写了如下代码:

def paint_unclosed_chart():
    workbook = xlsxwriter.Workbook('test.xlsx')
    worksheet = workbook.add_worksheet()
    workbook = record_report(workbook)
    unclosed_chart = workbook.add_chart({'type': 'scatter', 'subtype': 'straight_with_markers'})
    unclosed_chart.add_series({'values': '=数据处理!$A$2,数据处理!$B$1:$J$1,数据处理!$B$2:$J$2,1'})
    unclosed_chart.add_series({'values': '=数据处理!$A$3,数据处理!$B$1:$J$1,数据处理!$B$3:BJ$3,2'})
    worksheet.insert_chart('A1',unclosed_chart)
    workbook.close()

def record_report(workbook):#, date, unresolved_num, validate_num):
    worksheet = workbook.add_worksheet('数据处理')
    temp_data = [
            ['','10/20','10/21','10/24','10/25','10/26','10/27', '10/28', '10/31', '11/2'],
            ['待解决', '10', '11', '12', '13', '11', '10', '11', '9', '8'],
            ['待验证', '1', '4' , '2', '4', '6', '4', '4', '6', '8']
            ]
    worksheet.write_row('A1', temp_data[0])
    worksheet.write_row('A2', temp_data[1])
    worksheet.write_row('A3', temp_data[2])
    return workbook
    
if __name__ == '__main__':
    paint_unclosed_chart()

运行之后报错如下图:

网上这方面的资料有点少,我只排查到是在unclosed_chart.add_series的时候出错,找不到具体什么错误,想请问一下,是什么错误?谢谢~用的是python2.7

黄舟黄舟2767 days ago584

reply all(2)I'll reply

  • PHPz

    PHPz2017-04-18 09:57:51

    The add_series method of the chart has three parameters:
    name: represents the legend name;
    categories: is the x-axis item, which is the category;
    values: is the y-axis item, which is the value;
    You can get a Complete chart.

    reply
    0
  • 迷茫

    迷茫2017-04-18 09:57:51

    Try to set the name of the worksheet to English

    reply
    0
  • Cancelreply