XlsxWriter不能同时写入多个列

时间:2017-03-23 15:57:28

标签: python data-visualization pie-chart xlsxwriter

我正在尝试生成简单的饼图以更好地显示某些数据,但XlsxWriter不会同时将数据写入两列。其他例子的工作正常。

我对自己可能出错的地方一无所知

以下是数据:

{'core2': [10.3], 'core3': [4.17], 'core0': [58.68], 'core1': [24.42], 'core6': [0.02], 'core7': [0.0], 'core4': [2.31], 'core5': [0.12]})

实际数据作为列表传递 - > [10.3, 4.17, 58.68, 24.42, 0.02, 0.0, 2.31, 0.12]以下功能

请在下面找到代码:

def draw_simultaneously_busy_cores(type_of_chart,data,workbook):
    print data
    worksheet = workbook.add_worksheet()#'simultaneously_busy_cores')
    bold = workbook.add_format({'bold': 1})
    headings = [0, 1, 2, 3, 4, 5, 6, 7]
    worksheet.write_column('$A$1', headings, bold)
    worksheet.write_column('$B$1',headings)

    chart1 = workbook.add_chart({'type': type_of_chart})

    chart1.add_series({
         'name':       'Simultaneous Busy Cores',
         'categories': ['simultaneously_busy_cores', '=simultaneously_busy_cores!$A$1:$A$8'],
         'values':     ['simultaneously_busy_cores', '=simultaneously_busy_cores!$B$1:$B$8'],
         #'data_labels': {'percentage': True, }
         })
     #Add a title.
    chart1.set_title({'name': 'Simultaneous Busy Cores'})
     #Set an Excel chart style. Colors with white outline and shadow.
    chart1.set_style(10)
     #Insert the chart into the worksheet (with an offset).
    worksheet.insert_chart('C2', chart1, {'x_offset': 25, 'y_offset': 10})
提前谢谢。图片显示了输出:enter image description here

1 个答案:

答案 0 :(得分:1)

它应该工作。以下是示例数据的示例:

import xlsxwriter

def draw_simultaneously_busy_cores(type_of_chart, data, workbook):

    worksheet = workbook.add_worksheet('simultaneously_busy_cores')
    bold = workbook.add_format({'bold': 1})
    worksheet.write_column('A1', data, bold)
    worksheet.write_column('B1', data)

    chart1 = workbook.add_chart({'type': type_of_chart})

    chart1.add_series({
         'name':       'Simultaneous Busy Cores',
         'categories': '=simultaneously_busy_cores!$A$1:$A$8',
         'values':     '=simultaneously_busy_cores!$B$1:$B$8',
         })

    #Add a title.
    chart1.set_title({'name': 'Simultaneous Busy Cores'})

    #Set an Excel chart style. Colors with white outline and shadow.
    chart1.set_style(10)

    #Insert the chart into the worksheet (with an offset).
    worksheet.insert_chart('C2', chart1, {'x_offset': 25, 'y_offset': 10})


workbook = xlsxwriter.Workbook('test.xlsx')

data = [0, 1, 2, 3, 4, 5, 6, 7]    
draw_simultaneously_busy_cores('line', data, workbook)

workbook.close()

输出: enter image description here

示例中的图表categoriesvalues语法不正确。您正在混合列表和字符串语法。请再次阅读文档和示例。