import xlsxwriter
# Create a new Excel workbook and add a worksheet
workbook = xlsxwriter.Workbook( chart_with_trendline.xlsx )
worksheet = workbook.add_worksheet()
# Sample data
x_data = [1, 2, 3, 4, 5, 6, 7] # Adding more values to x_data
y_data = [5, 4, 3, 2, 1, 2, 3] # Adding more values to y_data
gearChange = 2 #index 2
line_x_data = []
line_y_data = []
line_x_data.append(x_data[gearChange])
line_y_data.append(y_data[gearChange])
line_x_data.append(x_data[gearChange])
line_y_data.append(0)
# Write data to the worksheet
worksheet.write_column( A1 , x_data)
worksheet.write_column( B1 , y_data)
# Write line data to worksheet
worksheet.write_column( C1 , line_x_data)
worksheet.write_column( D1 , line_y_data)
# Add a chart object
chart = workbook.add_chart({ type : scatter })
# Add scatter series to the chart
chart.add_series({
name : Y1 ,
categories : =Sheet1!$A$1:$A$7 ,
values : =Sheet1!$B$1:$B$7 ,
marker : { type : circle }
})
# Add line series to the chart
chart.add_series({
categories : =Sheet1!$C$1:$C$2 , # Adjusted for new data range
values : =Sheet1!$D$1:$D$2 , # Adjusted for new data range
line : { type : linear } # Adding linear trendline
})
# Set chart title and axis labels
chart.set_title({ name : Chart With Trendline })
chart.set_x_axis({ name : X Axis })
chart.set_y_axis({ name : Y Axis })
# Insert the chart into the worksheet
worksheet.insert_chart( E2 , chart)
# Close the workbook
workbook.close()
Here is my code at the moment.
I want to add the value 3 to the X-axis as this is the X intercept of the line I plot.
I ve looked through the documentation and I can t seem to find any function that allows me to do it. I ve also asked ChatGPT, but no luck there.
If there is a better way of plotting the vertical line at the specific gear change point that would be great too as I ve been stuck on this for a few days (part of a larger project). Any help will be really appreciated.
我试图改变这一行文:
chart.set_x_axis({名称:X Axis }>
因此,它想到这一点:
。
chart.set_x_axis({名称:X Axis , main_ay_ Values : line_x_data})
为了获得3个X-轴心,它做了一些工作。