I have an excel chart that has the following scheme
A B C D
ID Reading 1 Reading 2 Avg Reading 1
0 4.2 6.7 3.98
1 4.4 8.8
2 4.5 9
3 5.6 4
4 1.2 4.5
I'm able to draw a histogram with Reading 1 and Reading 2.
chart_1 = workbook.add_chart({'type': 'column'})
chart_1.add_series({
'name': '=Sheet1!$B$1',
'categories': '=Sheet1!$A$2:$A$4,
'values': '=Sheet1!$B$2:$B$4,
})
I want to overlay a line representing Avg Reading 1 across the histogram. How do I do this using python and xlsxwriter ?
Excel and XlsxWriter support Trendlines in charts. However, there is only a moving average option and not a fixed average option (which make sense given that it is meant to be for trends).
The usual way that you would do something like this in Excel is to add a line chart with the average on top of the histogram.
Here is a small working example based on your data:
Output:
Note, this adds an average point for each data point in the first series. It would be possible to just do this with two points if you prefer.
Also, rather that calculate the averages yourself you could let Excel do it using a formula. For example with this change to the above program:
See also the section of the docs on combined charts in XlsxWriter.