Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Add specific value to axis using Xlsxwriter

$
0
0
import xlsxwriter# Create a new Excel workbook and add a worksheetworkbook = xlsxwriter.Workbook('chart_with_trendline.xlsx')worksheet = workbook.add_worksheet()# Sample datax_data = [1, 2, 3, 4, 5, 6, 7]  # Adding more values to x_datay_data = [5, 4, 3, 2, 1, 2, 3]  # Adding more values to y_datagearChange = 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 worksheetworksheet.write_column('A1', x_data)worksheet.write_column('B1', y_data)# Write line data to worksheetworksheet.write_column('C1', line_x_data)worksheet.write_column('D1', line_y_data)# Add a chart objectchart = workbook.add_chart({'type': 'scatter'})# Add scatter series to the chartchart.add_series({'name': 'Y1','categories': '=Sheet1!$A$1:$A$7','values': '=Sheet1!$B$1:$B$7','marker': {'type': 'circle'}})# Add line series to the chartchart.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 labelschart.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 worksheetworksheet.insert_chart('E2', chart)# Close the workbookworkbook.close()

Output Excel File Picture

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.


Viewing all articles
Browse latest Browse all 23131

Trending Articles