Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 840
G Warner Avatar asked Feb 01 '26 18:02

G Warner


1 Answers

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

like image 170
jmcnamara Avatar answered Feb 04 '26 09:02

jmcnamara



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!