python - XlsxWriter 不同时写入多个列

标签 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

最佳答案

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

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语法不正确。您正在混合列表和字符串语法。再次阅读文档和示例。

关于python - XlsxWriter 不同时写入多个列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42981093/

相关文章:

python - 了解递归函数中内置于变量赋值中的多个递归调用

python - Bokeh 日期范围 slider

python - 文件未找到错误: [WinError 2] The system cannot find the file specified when using export_graphviz

javascript - 显示具有多个父节点的 D3 树

python - seaborn distplot y 轴密度值在几个 bin 上高于 1.0

d3.js - 如何从 d3 饼图中删除光标指针

python - 如何在 splinter 中选择 class , div , tag ?

python - SKlearn X 和 Y 的 reshape 警告

java - 如何在 MPAndroidChart 中的饼图中显示标签

c# - Windows Universal 10 中的饼图