xlsxwriter 组合图表类型
xlsxwriter combining chart types
我有一个 excel 图表,其中包含以下方案
A B C D
ID Reading 1 Reading 2 Avg Reading 1
0 4.2 6.7 3.98
1 4.4 8.8
2 4.5 9
3 5.6 4
4 1.2 4.5
我可以用读数 1 和读数 2 绘制直方图。
chart_1 = workbook.add_chart({'type': 'column'})
chart_1.add_series({
'name': '=Sheet1!$B',
'categories': '=Sheet1!$A:$A,
'values': '=Sheet1!$B:$B,
})
我想在直方图中叠加一条代表平均读数 1 的线。如何使用 python 和 xlsxwriter 执行此操作?
Excel 和 XlsxWriter 在图表中支持 Trendlines。但是,只有移动平均线选项,没有固定平均线选项(这是有道理的,因为它是为了趋势)。
在 Excel 中执行类似操作的通常方法是添加一个折线图,在直方图的顶部显示平均值。
这是一个基于您的数据的小示例:
from xlsxwriter.workbook import Workbook
workbook = Workbook('chart_combined.xlsx')
worksheet = workbook.add_worksheet()
# Add a format for the headings.
bold = workbook.add_format({'bold': True})
# Make the columns wider.
worksheet.set_column('B:C', 12)
# Add the worksheet data that the charts will refer to.
headings = ['ID', ' Reading 1', ' Avg Reading 1']
data = [
[0, 1, 2, 3, 4],
[4.2, 4.4, 4.5, 5.6, 1.2],
[3.98, 3.98, 3.98, 3.98, 3.98],
]
worksheet.write_row('A1', headings, bold)
worksheet.write_column('A2', data[0])
worksheet.write_column('B2', data[1])
worksheet.write_column('C2', data[2])
# Create a combined column and line chart that share the same X and Y axes.
# First create a column chart. This will use this as the primary chart.
column_chart = workbook.add_chart({'type': 'column'})
# Configure the data series for the primary chart.
column_chart.add_series({
'name': '=Sheet1!$B',
'categories': '=Sheet1!$A:$A',
'values': '=Sheet1!$B:$B',
})
# Create a line chart. This will use this as the secondary chart.
line_chart = workbook.add_chart({'type': 'line'})
# Configure the data series for the secondary chart.
line_chart.add_series({
'name': '=Sheet1!$C',
'categories': '=Sheet1!$A:$A',
'values': '=Sheet1!$C:$C',
})
# Combine the charts.
column_chart.combine(line_chart)
# Insert the chart into the worksheet
worksheet.insert_chart('E2', column_chart)
workbook.close()
输出:
注意,这会为第一个系列中的每个数据点添加一个平均点。如果您愿意,可以只用两点来做到这一点。
另外,您可以让 Excel 使用公式计算平均值,而不是自己计算平均值。例如,对上述程序进行此更改:
# Add the worksheet data that the charts will refer to.
headings = ['ID', ' Reading 1', ' Avg Reading 1']
data = [
[0, 1, 2, 3, 4],
[4.2, 4.4, 4.5, 5.6, 1.2],
['=AVERAGE($B:$B)'] * 5,
]
另请参阅 XlsxWriter 中 combined charts 的文档部分。
我有一个 excel 图表,其中包含以下方案
A B C D
ID Reading 1 Reading 2 Avg Reading 1
0 4.2 6.7 3.98
1 4.4 8.8
2 4.5 9
3 5.6 4
4 1.2 4.5
我可以用读数 1 和读数 2 绘制直方图。
chart_1 = workbook.add_chart({'type': 'column'})
chart_1.add_series({
'name': '=Sheet1!$B',
'categories': '=Sheet1!$A:$A,
'values': '=Sheet1!$B:$B,
})
我想在直方图中叠加一条代表平均读数 1 的线。如何使用 python 和 xlsxwriter 执行此操作?
Excel 和 XlsxWriter 在图表中支持 Trendlines。但是,只有移动平均线选项,没有固定平均线选项(这是有道理的,因为它是为了趋势)。
在 Excel 中执行类似操作的通常方法是添加一个折线图,在直方图的顶部显示平均值。
这是一个基于您的数据的小示例:
from xlsxwriter.workbook import Workbook
workbook = Workbook('chart_combined.xlsx')
worksheet = workbook.add_worksheet()
# Add a format for the headings.
bold = workbook.add_format({'bold': True})
# Make the columns wider.
worksheet.set_column('B:C', 12)
# Add the worksheet data that the charts will refer to.
headings = ['ID', ' Reading 1', ' Avg Reading 1']
data = [
[0, 1, 2, 3, 4],
[4.2, 4.4, 4.5, 5.6, 1.2],
[3.98, 3.98, 3.98, 3.98, 3.98],
]
worksheet.write_row('A1', headings, bold)
worksheet.write_column('A2', data[0])
worksheet.write_column('B2', data[1])
worksheet.write_column('C2', data[2])
# Create a combined column and line chart that share the same X and Y axes.
# First create a column chart. This will use this as the primary chart.
column_chart = workbook.add_chart({'type': 'column'})
# Configure the data series for the primary chart.
column_chart.add_series({
'name': '=Sheet1!$B',
'categories': '=Sheet1!$A:$A',
'values': '=Sheet1!$B:$B',
})
# Create a line chart. This will use this as the secondary chart.
line_chart = workbook.add_chart({'type': 'line'})
# Configure the data series for the secondary chart.
line_chart.add_series({
'name': '=Sheet1!$C',
'categories': '=Sheet1!$A:$A',
'values': '=Sheet1!$C:$C',
})
# Combine the charts.
column_chart.combine(line_chart)
# Insert the chart into the worksheet
worksheet.insert_chart('E2', column_chart)
workbook.close()
输出:
注意,这会为第一个系列中的每个数据点添加一个平均点。如果您愿意,可以只用两点来做到这一点。
另外,您可以让 Excel 使用公式计算平均值,而不是自己计算平均值。例如,对上述程序进行此更改:
# Add the worksheet data that the charts will refer to.
headings = ['ID', ' Reading 1', ' Avg Reading 1']
data = [
[0, 1, 2, 3, 4],
[4.2, 4.4, 4.5, 5.6, 1.2],
['=AVERAGE($B:$B)'] * 5,
]
另请参阅 XlsxWriter 中 combined charts 的文档部分。