openpyxl库散点图如何同时加入三组xy
时间: 2024-09-24 22:09:58 浏览: 36
sandiantu.zip_LABVIEW XY图_Labview的XY图_散点图
在Python中,使用openpyxl库创建Excel工作表并添加散点图通常用于数据分析。如果你想在同一张图表上绘制三组数据,你需要先加载数据,然后使用`openpyxl.drawing.chart.scatterChartData`类创建散点图,并添加多个系列(Series)。下面是一个简单的步骤示例:
```python
import openpyxl
from openpyxl.chart import ScatterChart, Reference
# 假设你已经有了三个数据列表,例如:
data_list1 = [(1, 2), (3, 4), (5, 6)] # Series 1
data_list2 = [(7, 8), (9, 10), (11, 12)] # Series 2
data_list3 = [(13, 14), (15, 16), (17, 18)] # Series 3
# 创建一个新的Excel workbook
workbook = openpyxl.Workbook()
sheet = workbook.active
# 添加数据到工作表
for i, data in enumerate([data_list1, data_list2, data_list3]):
for j, point in enumerate(data):
sheet.cell(row=j+1, column=i+1).value = point
# 创建图表区域
chart_data = Reference(sheet, min_col=1, min_row=1, max_col=len(data_list1)+1, max_row=len(data_list1) + len(data_list2) + len(data_list3))
# 创建散点图对象
chart = ScatterChart()
chart.title = 'Three Series Scatter Plot'
# 添加数据系列
series1 = chart.series.add(data=chart_data, titles_from_data=True, name="Series 1")
series2 = chart.series.add(data=chart_data, offset=2, titles_from_data=True, name="Series 2")
series3 = chart.series.add(data=chart_data, offset=4, titles_from_data=True, name="Series 3")
# 将图表添加到工作表
sheet.add_chart(chart, "E1")
# 保存工作簿
workbook.save("scatter_plot.xlsx")
阅读全文