1

i used this example to get started with pandas:

http://pandas-xlsxwriter-charts.readthedocs.io/chart_grouped_column.html#chart-grouped-column

i also want to save the chart in excel just like in the example

i would like to know how e.g. in the example above i can add a description or a table under the graph chart

the only thing related i found was this : Add graph description under graph in pylab

but this is done with pylab, is the same possible with pandas and an excel chart?

friggler
  • 177
  • 2
  • 14

1 Answers1

2

In Excel you could add a text box and insert some text but that isn't possible with XlsxWriter.

You could use the chart title property but in Excel the title is generally at the top and not the bottom.

You can reposition it, manually, in Excel. This is also possible with XlsxWriter using the layout options of the different chart objects.

Here is an example:

import xlsxwriter

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

# Create a new Chart object.
chart = workbook.add_chart({'type': 'column'})

# Write some data to add to plot on the chart.
data = [
    [1, 2, 3, 4, 5],
    [2, 4, 6, 8, 10],
    [3, 6, 9, 12, 15],
]

worksheet.write_column('A1', data[0])
worksheet.write_column('B1', data[1])
worksheet.write_column('C1', data[2])

# Configure the charts. In simplest case we just add some data series.
chart.add_series({'values': '=Sheet1!$A$1:$A$5'})
chart.add_series({'values': '=Sheet1!$B$1:$B$5'})
chart.add_series({'values': '=Sheet1!$C$1:$C$5'})

chart.set_x_axis({'name': 'X axis title'})
chart.set_y_axis({'name': 'Y axis title'})

chart.set_title({
    'name': 'Here is some text to describe the chart',
    'name_font':  {'bold': False, 'size': 10},
    'layout': {
        'x': 0.25,
        'y': 0.90,
    }
})

chart.set_plotarea({
    'layout': {
        'x':      0.11,
        'y':      0.10,
        'width':  0.75,
        'height': 0.60,    
    }
})

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

workbook.close()

Note, you will need to do some trial and error with the layout property to get the layout that you want.

Output:

enter image description here

jmcnamara
  • 38,196
  • 6
  • 90
  • 108