I am trying to created stacked line charts in excel using pandas and xlsxwriter.
When entering the chart type dict into add chart I use what the documentation states should configure a stacked line in Excel. (So I think anyway!) When I open Excel I get a standard line chart, which means I need to manually change to stacked line (the second across box)
Here's some example code
import pandas as pd
# example csv data
'''
Date-Time col1 col2
2021-03-01 00:00:00 34329 34540
2021-03-01 00:15:00 34174 34369
2021-03-01 00:30:00 34121 34418
2021-03-01 00:45:00 34012 34235
2021-03-01 01:00:00 33959 34273
2021-03-01 01:15:00 33825 34049
2021-03-01 01:30:00 33782 34010
2021-03-01 01:45:00 33584 33882
2021-03-01 02:00:00 33601 33905
2021-03-01 02:15:00 33415 33746
2021-03-01 02:30:00 33412 33827
2021-03-01 02:45:00 33291 33744
2021-03-01 03:00:00 33329 33816
2021-03-01 03:15:00 33209 33745
2021-03-01 03:30:00 33219 33833
'''
# create df
with open('example.csv', 'r') as csv:
df = pd.read_csv(csv).set_index('Date-Time')
# Create the workbook to save the data within
workbook = pd.ExcelWriter('example.xlsx', engine='xlsxwriter')
# Create sheets in excel for data
pd.DataFrame().to_excel(workbook, sheet_name='Dashboard')
# assign a blank dashboard for the charts
worksheet_dashboard = workbook.sheets['Dashboard']
# Get the xlsxwriter objects from the dataframe writer object
# for use with creating charts later
book = workbook.book
# Create sheets in excel for data
df.to_excel(workbook, sheet_name='example')
# Add the line chart objects
chart = book.add_chart({'type': 'line', 'subtype': 'stacked'})
# Configure the first series.
chart.add_series({
'name': '=example!$B$1',
'categories': '=example!$A$1:$A$16',
'values': '=example!$B$2:$B$16',
})
chart.add_series({
'name': '=example!$C$1',
'categories': '=example!$A$1:$A$16',
'values': '=example!$C$2:$C$16',
})
# Insert the chart into the worksheet
worksheet_dashboard.insert_chart('A1', chart)
workbook.save()
You can see that the line
# Add the line chart objects
chart = book.add_chart({'type': 'line', 'subtype': 'stacked'})
Should provide the type as line and subtype stacked, anyone encountered this problem before?
As a note the versions I'm using:
- Microsoft Office 365 Excel version 2008.
- print(pd.version) outputs: 0.23.0

