Pilfer Ahn
Pilfer Ahn

Reputation: 91

How Could I apply more than two Categories(X-axis) in XlsxWirter?

I Would like to draw the chart using 2 different catetoreis (x-axis) with 1 values (y-axis) in one chart. But, currently I could not get the 2 different chart at one figure.

x1 = [6.60 ,6.87 ,7.06 ,6.62 ,7.28]
x2 = [4.29 ,5.14 ,5.34 ,4.57 ,4.81]
p1 = [30.95 ,30.93 ,31.13 ,30.98 ,31.17 ]

worksheet.write_column('A1', x1)
worksheet.write_column('B1', x2)

# categories
worksheet.write_column('C1', p1)

chart1 = workbook.add_chart({'type': 'line'})

chart1.add_series ({
    'categories' : '=Sheet1!$A$1:$A$5',
    'values' : 'Sheet1!$C$1:$C$5',
    'line':   {'none': True},
    'marker': {'type': 'automatic'},  
})

chart1.add_series ({
    'categories' : '=Sheet1!$B$1:$B$5',
    'values' : 'Sheet1!$C$1:$C$5',
    'line':   {'none': True},
    'marker': {'type': 'automatic'},  
})

worksheet.insert_chart('E1,' , chart1)

enter image description here

I expect to get above figure. However, when I run the code i got below figure. It's only takes first Categories(x-axis) and I could not see the other one.It's draw at same point for x-axis. Is there any way to figure it out like above figure?

enter image description here

Upvotes: 1

Views: 80

Answers (1)

jmcnamara
jmcnamara

Reputation: 41644

That is the default behaviour for line charts in Excel. I think what you are looking for is a Scatter chart. Here is an example, with the y-axis range set:

from xlsxwriter import Workbook

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

x1 = [6.60 ,6.87 ,7.06 ,6.62 ,7.28]
x2 = [4.29 ,5.14 ,5.34 ,4.57 ,4.81]
p1 = [30.95 ,30.93 ,31.13 ,30.98 ,31.17 ]

worksheet.write_column('A1', x1)
worksheet.write_column('B1', x2)

worksheet.write_column('C1', p1)

chart1 = workbook.add_chart({'type': 'scatter'})
chart1.set_y_axis({'min': 22, 'max': 32})

chart1.add_series ({
    'categories' : '=Sheet1!$A$1:$A$5',
    'values' : 'Sheet1!$C$1:$C$5',
    'line':   {'none': True},
    'marker': {'type': 'automatic'},  
})

chart1.add_series ({
    'categories' : '=Sheet1!$B$1:$B$5',
    'values' : 'Sheet1!$C$1:$C$5',
    'line':   {'none': True},
    'marker': {'type': 'automatic'},  
})

worksheet.insert_chart('E1,' , chart1)

workbook.close()

Output: enter image description here

Upvotes: 1

Related Questions