回复 楼主 frefly0203
程序代码:
from openpyxl import Workbook
from openpyxl.chart import (
ScatterChart,
Reference,
Series,
)
wb = Workbook()
ws = wb.active
rows = [
['Size', 'Batch 1', 'Batch 2'],
[2, 40, 30],
[3, 40, 25],
[4, 50, 30],
[5, 30, 25],
[6, 25, 35],
[7, 20, 40],
]
for row in rows:
ws.append(row)
chart = ScatterChart()
chart.title = "Scatter Chart"
chart.style = 10
chart.x_axis.title = 'Size'
chart.y_axis.title = 'Percentage'
xvalues = Reference(ws, min_col=1, min_row=2, max_row=7)
for i in range(2, 4):
values = Reference(ws, min_col=i, min_row=1, max_row=7)
series = Series(values, xvalues, title_from_data=True)
chart.series.append(series)
# 第一条散点
s1 = chart.series[0]
# 散点标记类型 'auto', 'dash', 'triangle', 'square', 'picture', 'circle', 'dot', 'plus', 'star', 'diamond', 'x'
s1.marker.symbol = "circle"
s1.marker.graphicalProperties.solidFill = "0000FF" # Marker filling 设定标记填充的颜色
s1.marker.graphicalProperties.line.solidFill = "0000FF" # Marker outline 标记轮廓的颜色
s1.graphicalProperties.line.noFill = True # 关闭连线填充
# 第二条带连线的散点
s2 = chart.series[1]
s2.marker.symbol = "circle"
s2.graphicalProperties.solidFill = "FF0000"
s2.marker.graphicalProperties.line.solidFill = "FF0000"
s2.graphicalProperties.dashStyle = "dash"
s2.graphicalProperties.line.width = 5000 #控制线条的粗细
ws.add_chart(chart, "A10")
wb.save("scatter1.xlsx")