我正在尝试将标记添加到我在xlsxwriter for Python中创建的组合图表中。在我合并了一个直线图和柱形图后,我想在Val_1 = Val_2的点上放置一个圆形标记。我无法弄清楚如何在xlsxwriter中执行此操作。我可以在Excel中执行此操作:
数据:
Date Val_1 Val_2 Flag
1-Jan 100 50 #N/A
2-Jan 150 250 #N/A
3-Jan 125 100 #N/A
4-Jan 110 110 110
5-Jan 170 225 #N/A
在之后我将图表与“#34; .add_series"但我的图表没有显示任何标记。我觉得这与组合图表有关。有人会有一个如何做到这一点的例子吗?
由于
答案 0 :(得分:3)
当然可以通过添加带标记的第二个系列而不显示线来显示值匹配的点。
以下是一个例子:
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})
# Add the worksheet data that the charts will refer to.
headings = ['Number', 'Batch 1', 'Batch 2', 'Equal']
data = [
[2, 3, 4, 5, 6, 7 ],
[10, 40, 50, 20, 10, 50 ],
[30, 40, 70, 50, 10, 30 ],
['=NA()', 40, '=NA()', '=NA()', 10, '=NA()'],
]
worksheet.write_row('A1', headings, bold)
worksheet.write_column('A2', data[0])
worksheet.write_column('B2', data[1])
worksheet.write_column('C2', data[2])
worksheet.write_column('D2', data[3])
# Create a new 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$1',
'categories': '=Sheet1!$A$2:$A$7',
'values': '=Sheet1!$B$2:$B$7',
})
# Create a new 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$1',
'categories': '=Sheet1!$A$2:$A$7',
'values': '=Sheet1!$C$2:$C$7',
})
# Add a series to represent the intersection points, with markers only.
line_chart.add_series({
'name': '=Sheet1!$D$1',
'categories': '=Sheet1!$A$2:$A$7',
'values': '=Sheet1!$D$2:$D$7',
'marker': {'type': 'circle', 'size': 10},
'line': {'none': True},
})
# Delete/hide series 2 from the legend.
column_chart.set_legend({'delete_series': [2]})
# Combine the charts.
column_chart.combine(line_chart)
# Add a chart title and some axis labels. Note, this is done via the
# primary chart.
column_chart.set_title({ 'name': 'Combined chart'})
column_chart.set_x_axis({'name': 'Test number'})
column_chart.set_y_axis({'name': 'Sample length (mm)'})
# Insert the chart into the worksheet
worksheet.insert_chart('F2', column_chart)
workbook.close()
输出:
注意,此示例隐藏了图例中的“Equal”系列名称。我不知道你是否需要它,但我添加它是为了完整性。如果你不需要,请省略它。