G Warner
G Warner

Reputation: 1379

Categorical values on the x-axis with xlsxwriter

I have a plot that needs names as the values for the x-axis. I imagine this is accomplished via the set_x_axis function for chart objects but can't find the proper key in the documentation (http://xlsxwriter.readthedocs.io/chart.html#set_x_axis). The following code produces the graph below:

chart = workbook.add_chart({'type':'scatter'})
colLetter = alphabet[1] #alphabet is list of alphabet
for ii in range(4):
    colLetter = alphabet[ii+1]
    chart.add_series({
        'name': '=Sheet1!$%s$1'%colLetter,
        'categories': '=Sheet1!$A$2:$A$'+str(lastRowNumber),
        'values': '=Sheet1!$%s$2:$%s$6'%(colLetter, colLetter),
    })
chart.set_title({'name': 'Cityblock'})
chart.set_x_axis({'name': 'Subject'})
chart.set_y_axis({'name': 'Distance'})
chart.set_style(11)
worksheet.insert_chart('F1', chart)

enter image description here

Any suggestions? I'm using xlsxwriter with python 2.7.

Upvotes: 3

Views: 3110

Answers (1)

jmcnamara
jmcnamara

Reputation: 41644

Just set the series categories to point to the strings, or numbers, or dates that you wish to plot.

For example:

import xlsxwriter

workbook = xlsxwriter.Workbook('chart.xlsx')
worksheet = workbook.add_worksheet()

# Add a column chart.
chart = workbook.add_chart({'type': 'column'})

# Write some data to add to plot on the chart.
worksheet.write_column('A1', ['Bob', 'Eve', 'Ann'])
worksheet.write_column('B1', [5, 10, 7])

# Configure the chart.
chart.add_series({'categories': '=Sheet1!$A$1:$A$3',
                  'values':     '=Sheet1!$B$1:$B$3'})

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

workbook.close()

Output:

enter image description here

Upvotes: 3

Related Questions