gpt4 book ai didi

pandas xlsxwriter 堆积条形图

转载 作者:行者123 更新时间:2023-12-01 15:18:36 26 4
gpt4 key购买 nike

我想在 excel 中上传分组条形图,但我似乎找不到这样做的方法。

这是我的代码:

bar_chart2 = workbook.add_chart({'type':'column'})
bar_chart2.add_series({
'name':'Month over month product',
'categories':'=Month over month!$H$2:$H$6',
'values':'=Month over month!$I$2:$J$6',
})
bar_chart2.set_legend({'none': True})

worksheet5.insert_chart('F8',bar_chart2)

bar_chart2.set_legend({'none': True})

worksheet5.insert_chart('F8',bar_chart2)

不过,我明白了。 enter image description here

最佳答案

使用您提供的数据,我重新编写了 jmcnamara 文档中给出的示例(链接 here)以满足您的需求。

完整代码:

import pandas as pd
import xlsxwriter

headings = [' ', 'Apr 2017', 'May 2017']
data = [
['NGN', 'UGX', 'KES', 'TZS', 'CNY'],
[5816, 1121, 115, 146, 1],
[7089, 1095, 226, 120, 0],
]

#opening workbook
workbook = xlsxwriter.Workbook("test.xlsx")

worksheet5 = workbook.add_worksheet('Month over month')

worksheet5.write_row('H1', headings)
worksheet5.write_column('H2', data[0])
worksheet5.write_column('I2', data[1])
worksheet5.write_column('J2', data[2])

# beginning of OP snippet
bar_chart2 = workbook.add_chart({'type':'column'})

bar_chart2.add_series({
'name': "='Month over month'!$I$1",
'categories': "='Month over month'!$H$2:$H$6",
'values': "='Month over month'!$I$2:$I$6",
})

bar_chart2.add_series({
'name': "='Month over month'!$J$1",
'categories': "='Month over month'!$H$2:$H$6",
'values': "='Month over month'!$J$2:$J$6",
})

bar_chart2.set_title ({'name': 'Month over month product'})
bar_chart2.set_style(11)

#I took the liberty of leaving the legend in there - it was commented in originally
#bar_chart2.set_legend({'none': True})

# end of OP snippet

worksheet5.insert_chart('F8', bar_chart2)

workbook.close()

输出: enter image description here

关于pandas xlsxwriter 堆积条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45441951/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com