English 中文(简体)
用Xlsx作家向轴线添加具体价值
原标题:Add specific value to axis using Xlsxwriter
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()

“Output

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-轴心,它做了一些工作。

问题回答

仅将主要单位定在1个,轴数为1-8个。

添加轴心

chart.set_x_axis({ name :  X Axis })
chart.set_x_axis({
     name :  X Axis ,
     major_unit : 1,  # set major unit to 1
})

“Chart


You could also add a label to the Trend Line like;
# 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
     data_labels : { value : False,     # Add Data Label
                     position :  below ,
                     category : True,
                    }
})

All this is doing is labeling your trend line, 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 enter image description here


You could add a verical 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.




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签