xlsxwriter - 使用 xlswriter 向条形图添加趋势线
问题描述
我正在尝试创建带有趋势线的条形图。我可以在 excel 中执行此操作,并希望使该过程自动化。xlswriter 非常易于使用,我已经复制了条形图,它只是对我不起作用的趋势线。似乎在该行中添加了 2 个元素,并在每个堆栈的顶部添加了一个附加栏。
这是创建左侧图表的代码
import xlsxwriter
# create worbook, workseet and chart
workbook = xlsxwriter.Workbook("Example.xlsx")
worksheet = workbook.add_worksheet()
chart1 = workbook.add_chart({'type': 'column', 'subtype': 'stacked'})
# Add the worksheet data
headings = ['Model 1', 'Model 2', 'Capacity']
data = [
[10, 40, 50, 20, 10, 50],
[30, 60, 70, 50, 40, 30],
[20, 30, 40, 40, 30, 30]
]
worksheet.write_row('A1', headings)
worksheet.write_column('A2', data[0])
worksheet.write_column('B2', data[1])
worksheet.write_column('C2', data[2])
# Configure the first series.
chart1.add_series({
'name': '=Sheet1!$A$1',
'values': '=Sheet1!$A$2:$A$7',
})
# Configure the first series.
chart1.add_series({
'name': '=Sheet1!$B$1',
'values': '=Sheet1!$B$2:$B$7',
})
chart1.add_series({
'name': '=Sheet1!$C$1',
'values': '=Sheet1!$C$2:$C$7',
'trendline': {'type': 'linear'},
})
# Set an Excel chart style.
chart1.set_style(11)
# Add a chart title
chart1.set_title ({'name': 'xlsxwriter chart'})
# Insert the chart into the worksheet (with an offset).
worksheet.insert_chart('F1', chart1)
# Finally, close the Excel file
workbook.close()
条形图选择我尝试插入的数据作为趋势线。任何帮助,将不胜感激。
解决方案
看起来您要做的是添加辅助折线图而不是趋势线。您可以使用 XlsxWriter chart.combine()方法执行此操作。
像这样:
import xlsxwriter
# create worbook, workseet and chart
workbook = xlsxwriter.Workbook("Example.xlsx")
worksheet = workbook.add_worksheet()
chart1 = workbook.add_chart({'type': 'column', 'subtype': 'stacked'})
# Add the worksheet data
headings = ['Model 1', 'Model 2', 'Capacity']
data = [
[10, 40, 50, 20, 10, 50],
[30, 60, 70, 50, 40, 30],
[20, 30, 40, 40, 30, 30]
]
worksheet.write_row('A1', headings)
worksheet.write_column('A2', data[0])
worksheet.write_column('B2', data[1])
worksheet.write_column('C2', data[2])
# Configure the first series.
chart1.add_series({
'name': '=Sheet1!$A$1',
'values': '=Sheet1!$A$2:$A$7',
})
# Configure the first series.
chart1.add_series({
'name': '=Sheet1!$B$1',
'values': '=Sheet1!$B$2:$B$7',
})
# Add a chart title
chart1.set_title ({'name': 'xlsxwriter chart'})
# Create a second line chart.
chart2 = workbook.add_chart({'type': 'line'})
chart2.add_series({
'name': '=Sheet1!$C$1',
'values': '=Sheet1!$C$2:$C$7',
})
# Combine the charts.
chart1.combine(chart2)
# Insert the chart into the worksheet (with an offset).
worksheet.insert_chart('F1', chart1)
# Finally, close the Excel file
workbook.close()
输出:
推荐阅读
- python - 如何在数据框中展开列,其中包含需要在熊猫中展开的每一行的完整列表?
- laravel - 使用 Laravel 启动 Docker 容器时,*有时*会触发导致 HTTP 500 的“类视图不存在”
- google-sheets - 阻止其他用户使用 Google 脚本
- oracle - 重命名查询中使用的表时,物化视图无效。ORA-00942 和 ORA-06512,oracle 19c
- cypress - 缺少 APPLITOOLS_API_KEY 环境变量
- android - Glide 可以在 Android TextView 中渲染 ImageSpan 吗?
- python - 如何在 embed mod 中更改 Ipython 输入/输出提示
- python - 根据标题python的名称和数量订购原始数据
- snowflake-cloud-data-platform - 我们正在尝试将 ACCOUNTADMIN 角色分配给用户,但由于某种原因,它似乎没有生效或更新..不知道为什么?
- .net - 为什么 Visual Studio NugetPackageManager 中的“版本”列对于 .NET Framework 项目显示为空白?