Here's a scatter plot :
All Im trying to do is simply add a line through the point -.8 (y axis) , to .4 (x axis) , which crosses trough (0,0), but this is what I end up getting:
Here's my code:
diagonal = workbook.add_chart({'type':'line'})
###for line
worksheet.write(0,0,-.8)
worksheet.write(1,0,.4)
###net and gain are just excel columns (for scatterplot)
diagonal.add_series({'values':['sheet',0,0,1,0],'categories':net)
scatter = workbook.add_chart({'type':'scatter'})
scatter.add_series({'values':gain,'categories':net)
diagonal.combine(scatter)
worksheet.insert_chart(1,1,diagonal)
You would probably get the same result if you combined a line and scatter chart in Excel. Somewhat confusingly a "line" chart in Excel is based on fixed division categories. What you probably need is a to plot a second "scatter" series for the line.
Something like this:
import xlsxwriter
workbook = xlsxwriter.Workbook('chart_scatter.xlsx')
worksheet = workbook.add_worksheet()
bold = workbook.add_format({'bold': 1})
# Add the worksheet data that the charts will refer to.
worksheet.write_column('A1', [-0.2, 0.3, -0.4, 0.15, -0.6, 0.37])
worksheet.write_column('B1', [0.1, -0.4, -0.5, -0.25, 0.1, -0.51])
worksheet.write_column('C1', [-0.8, 0.4])
worksheet.write_column('D1', [ 0, 0])
# Create a new scatter chart.
chart = workbook.add_chart({'type': 'scatter'})
# Add the scatter data.
chart.add_series({
'categories': ['Sheet1', 0, 0, 5, 0],
'values': ['Sheet1', 0, 1, 5, 1],
'marker': {
'type': 'circle',
'border': {'color': 'red'},
'fill': {'color': 'red'},
},
})
# Add the straight lines series.
chart.add_series({
'categories': ['Sheet1', 0, 2, 1, 2],
'values': ['Sheet1', 0, 3, 1, 3],
'line': {'color': 'blue'},
'marker': {'type': 'none'},
})
# Format the chart.
chart.set_x_axis({'min': -0.8, 'max': 0.4})
chart.set_legend({'none': True})
worksheet.insert_chart('D5', chart)
workbook.close()
Output:
The key here is to figure out what you want to do in Excel first and then try apply that to an XlsxWriter program.