I am trying to add statistics to my charts. Now the first image does this fine. the second image you cant see the markers but they are there. How does one make this markers visible. Any ideas what I am doing wrong?
import pandas as pd
import matplotlib.pyplot as plt
import openpyxl
from openpyxl.utils.dataframe import dataframe_to_rows
from openpyxl.chart import ScatterChart, Reference, Series
from openpyxl import Workbook
import scipy.stats as stats
Read the data from Excel file
df = pd.read_excel(“yield vs CWP_original.xlsx”, skiprows=[1], usecols=[1, 4])
x = df[“CWP”]
y = df[“YIELD”]
Perform linear regression
slope, intercept, r_value, p_value, std_err = stats.linregress(x, y)
r_squared = r_value ** 2
Create the scatter plot with marker options
plt.scatter(x, y, marker=‘o’, label=‘Data Points’, color=‘blue’)
plt.plot(x, slope * x + intercept, color=‘red’, label=‘Regression Line’)
plt.xlabel(‘CWP’)
plt.ylabel(‘YIELD’)
plt.title(‘Yield vs CWP’)
Add equation and R-squared value to the graph
equation_text = f"YIELD = {slope:.2f} * CWP + {intercept:.2f}"
r_squared_text = f"R-squared = {r_squared:.4f}"
plt.text(0.05, 0.95, equation_text, transform=plt.gca().transAxes,
fontsize=12, verticalalignment=‘top’)
plt.text(0.05, 0.88, r_squared_text, transform=plt.gca().transAxes,
fontsize=12, verticalalignment=‘top’)
plt.show()
Create a new Excel workbook and add the data
wb = Workbook()
sheet = wb.active
for r in dataframe_to_rows(df, index=False, header=True):
sheet.append(r)
Create the scatter chart
chart = ScatterChart()
chart.title = “Yield vs CWP”
xvalues = Reference(sheet, min_col=1, min_row=2,
max_row=len(df) + 1, max_col=1)
yvalues = Reference(sheet, min_col=2, min_row=1,
max_row=len(df) + 1, max_col=2)
series = Series(yvalues, xvalues, title_from_data=True)
chart.series.append(series)
Add equation and R-squared value as text on the chart
sheet.cell(row=len(df) + 4, column=1, value=equation_text)
sheet.cell(row=len(df) + 5, column=1, value=r_squared_text)
You can change the cell reference (E2) as per your preference
sheet.add_chart(chart, “J2”)
Save the modified Excel file
wb.save(“yield vs CWP_original_with_chart.xlsx”)