我想用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
试试把worksheet的名字设成英文
图表的add_series方法有三个参数:
name:代表图例名称;
categories:是x轴项,也就是类别;
values:是y轴项,也就是值;
三个参数填写完整,才能得到一个完整的图表。