openpyxl趋势线和R平方值

我试图添加“线性”趋势线到我的Excel图表,并显示R平方值使用openpyxl,但我找不到任何示例。

下面是生成图像上显示的图表的python代码,没有趋势线和R平方公式图表图像 。

谢谢!

from openpyxl import Workbook, load_workbook from openpyxl.chart import ( ScatterChart, Reference, Series, ) from openpyxl.chart.trendline import Trendline wb = load_workbook(r"path to load blank workbook\data.xlsx") ws = wb.active rows = [ ['Size', 'Batch 1'], [3, 40], [4, 50], [2, 40], [5, 30], [6, 25], [7, 20], ] for row in rows: ws.append(row) chart = ScatterChart() chart.title = "Scatter Chart" #chart.style = 13 chart.x_axis.title = 'Size' chart.y_axis.title = 'Percentage' xvalues = Reference(ws, min_col=1, min_row=2, max_row=8) for i in range(2, 4): values = Reference(ws, min_col=i, min_row=2, max_row=8) series = Series(values, xvalues, title_from_data=True) chart.series.append(series) line = chart.series[0] line.graphicalProperties.line.noFill = True line.marker.symbol = "circle" ws.add_chart(chart, "A10") wb.save("path to save workbook\scatter.xlsx") 

logging图表的所有可能性基本上是不可能的,所以你偶尔不得不深入到相关图表的XML中,以了解它是如何完成的。 也就是说,趋势线很容易做到。

 from openpyxl.chart.trendline import Trendline line.trendline = Trendline()