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.
I tried changing the line:
chart.set_x_axis({'name': 'X Axis'})
So that it looks like this:
chart.set_x_axis({'name': 'X Axis', 'major_tick_values': line_x_data})
In order to get 3 on the X-axis but it didn't work.

Just set the major unit to 1 so the axis counts 1 - 8 by 1s.
Add to the x-axis setting
You could also add a label to the series like;
All this is doing is labelling your series, with the lower label sitting on the X-Axis.
You'll notice the value is set at start and end points but I don't think there is any means to remove the top textbox using Xlsxwriter. It can be removed manually however simply by clicking the textbox twice and then use the delete key.
And for that matter you could manually move the bottom textbox to align with the other numbers too if you like
Other options to add vert line
You could add a vertical line using Error Bars but it doesn't give you any addition features. In fact you would have to use the major unit change to see the 3 on the x-axis value since it has no data label.
Apart from that I suppose you could just add a drawing line/textbox on to the Chart.