Gohawks
Gohawks

Reputation: 1134

xlsxwriter Marker in Combined Chart

I am trying to add a marker to a combined chart that I have created in xlsxwriter for Python. After I have combined a line and column chart, I want to put a circular marker on points where Val_1 = Val_2. I can't figure out how to do this in xlsxwriter. I can do it in Excel, however:

Data:

 Date   Val_1   Val_2   Flag
 1-Jan  100      50     #N/A
 2-Jan  150      250    #N/A
 3-Jan  125      100    #N/A
 4-Jan  110      110    110
 5-Jan  170      225    #N/A

enter image description here

After I combine the charts I try and ".add_series" but my chart does not display any markers. I have a feeling this has something to do with combining the charts. Would anyone have an example of how to do this?

Thanks

Upvotes: 3

Views: 1829

Answers (1)

jmcnamara
jmcnamara

Reputation: 41644

That is certainly possible by adding a second line series with markers and no line to show the points where the values match.

Here is an example:

from xlsxwriter.workbook import Workbook

workbook  = Workbook('chart_combined.xlsx')
worksheet = workbook.add_worksheet()

# Add a format for the headings.
bold = workbook.add_format({'bold': True})

# Add the worksheet data that the charts will refer to.
headings = ['Number', 'Batch 1', 'Batch 2', 'Equal']
data = [
    [2,        3,      4,        5,          6,    7      ],
    [10,       40,     50,       20,        10,    50     ],
    [30,       40,     70,       50,        10,    30     ],
    ['=NA()',  40,     '=NA()',  '=NA()',   10,    '=NA()'],
]

worksheet.write_row('A1', headings, bold)
worksheet.write_column('A2', data[0])
worksheet.write_column('B2', data[1])
worksheet.write_column('C2', data[2])
worksheet.write_column('D2', data[3])

# Create a new column chart. This will use this as the primary chart.
column_chart = workbook.add_chart({'type': 'column'})

# Configure the data series for the primary chart.
column_chart.add_series({
    'name':       '=Sheet1!$B$1',
    'categories': '=Sheet1!$A$2:$A$7',
    'values':     '=Sheet1!$B$2:$B$7',
})

# Create a new line chart. This will use this as the secondary chart.
line_chart = workbook.add_chart({'type': 'line'})

# Configure the data series for the secondary chart.
line_chart.add_series({
    'name':       '=Sheet1!$C$1',
    'categories': '=Sheet1!$A$2:$A$7',
    'values':     '=Sheet1!$C$2:$C$7',
})

# Add a series to represent the intersection points, with markers only.
line_chart.add_series({
    'name':       '=Sheet1!$D$1',
    'categories': '=Sheet1!$A$2:$A$7',
    'values':     '=Sheet1!$D$2:$D$7',
    'marker':     {'type': 'circle', 'size': 10},
    'line':       {'none': True},
})

# Delete/hide series 2 from the legend.
column_chart.set_legend({'delete_series': [2]})

# Combine the charts.
column_chart.combine(line_chart)

# Add a chart title and some axis labels. Note, this is done via the
# primary chart.
column_chart.set_title({ 'name': 'Combined chart'})
column_chart.set_x_axis({'name': 'Test number'})
column_chart.set_y_axis({'name': 'Sample length (mm)'})

# Insert the chart into the worksheet
worksheet.insert_chart('F2', column_chart)

workbook.close()

Output:

enter image description here

Note, this example hides the "Equal" series name from the legend. I don't know if you need that but I added it for completeness. Omit it if you don't need it.

Upvotes: 5

Related Questions