如果在Excel中结合使用折线图和散点图,可能会得到相同的结果。有些令人困惑的是,Excel中的“折线图”是基于固定的划分类别。您可能需要的是绘制线的第二个“散射”序列。
类似这样:
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()
输出:
这里的关键是先找出您想在Excel中做什么,然后尝试将其应用到XlsxWriter程序中。