gpt4 book ai didi

python - 组合图表中的 xlsxwriter 标记

转载 作者:太空宇宙 更新时间:2023-11-04 01:07:09 26 4
gpt4 key购买 nike

我正在尝试将标记添加到我在 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

enter image description here

之后 我结合了我尝试的图表和“.add_series”,但我的图表不显示任何标记。我觉得这与组合图表有关。谁能举例说明如何执行此操作?

谢谢

最佳答案

这当然可以通过添加带有标记但没有线来显示值匹配的点的第二行系列来实现。

这是一个例子:

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()

输出:

enter image description here

请注意,此示例隐藏了图例中的“Equal”系列名称。我不知道你是否需要它,但为了完整性我添加了它。如果不需要,请忽略它。

关于python - 组合图表中的 xlsxwriter 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29779095/

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