Chart Examples

Column Chart

Create a simple column chart:

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

# Configure the series of the chart from the dataframe data.
chart.add_series({
   'values':     '=Sheet1!$B$2:$B$8',
   'gap':        2,
})

# Configure the chart axes.
chart.set_y_axis({'major_gridlines': {'visible': False}})

# Turn off chart legend. It is on by default in Excel.
chart.set_legend({'position': 'none'})
...

Full example code.

The output from this program exported from Excel as an image is:

_images/chart_column.png

Instead of the Excel style range notation, you can use the following list syntax which is easier to create programmatically:

chart.add_series({
    'values':     ['Sheet1', 1, 1, 7, 1],
    'gap':        2,
})

Excel refers to this type of histogram chart as “Column” charts.

Axis Labels

Adding labels to the chart axes is easy:

chart.set_x_axis({'name': 'Index'})
chart.set_y_axis({'name': 'Value', 'major_gridlines': {'visible': False}})
_images/chart_axis_labels.png

Full example code.

You can also rotate the axis numbers:

chart.set_x_axis({'name': 'Index', 'num_font':  {'rotation': 45}})
_images/chart_axis_labels_rotated.png

Full example code.

Line Chart

Create a simple Line chart:

chart = workbook.add_chart({'type': 'line'})
_images/chart_line.png

Full example code.

Legends

Excel adds a legend to a chart by default:

_images/chart_legend.png

Full example code.

We can also turn the chart legend off, like the previous examples, or position the legend.

The following is an example using stock data and positioning the legend at the top of the chart:

chart.set_legend({'position': 'top'})
_images/chart_legend_stock.png

Full example code.

Scatter Chart

Create a simple scatter chart.

Rather than use Excel’s default symbols for each data series we set each one to be a circle:

chart.add_series({
  # ...
  'marker':     {'type': 'circle', 'size': 7},
})
_images/chart_scatter.png

Full example code.

Colours

Colours are configurable for almost all aspects of XlsxWriter charts. In the following example we borrow the Color Brewer colours from Vincent and apply them to a scatter chart:

_images/chart_colors.png

Full example code.

Area Chart

Create a simple Area chart:

chart = workbook.add_chart({'type': 'area'})
_images/chart_area.png

Full example code.

Stacked Area Chart

A Stacked Area chart is a subtype of an Area chart in Excel:

chart = workbook.add_chart({'type': 'area', 'subtype': 'stacked'})
_images/chart_stacked_area1.png

Full example code.

Or with more categories and the ‘Spectral’ colorbrew scheme from Vincent:

_images/chart_stacked_area2.png

Full example code.

Or with stock data and the ‘Accent’ colorbrew scheme:

_images/chart_stacked_area3.png

Full example code.

Stacked Column Chart

A Stacked Column chart is a subtype of an Column chart in Excel:

chart = workbook.add_chart({'type': 'column', 'subtype': 'stacked'})
_images/chart_stacked_column.png

Full example code.

Or with different data and a wider gap:

_images/chart_stacked_column_farms.png

Full example code.

Grouped Column Chart

A Grouped Column chart is the default Column chart in Excel:

chart = workbook.add_chart({'type': 'column'})
_images/chart_grouped_column.png

Full example code.

Or with the farm data from above:

_images/chart_stacked_column_farms.png

Full example code.

Pie Chart

Create a simple Pie chart with user defined colours:

chart = workbook.add_chart({'type': 'pie'})
_images/chart_pie.png

Full example code.

Chart Images

The images shown above were all exported from Excel for Mac 2011 using files created with Pandas and XlsxWriter.

The example programs and output files are on GitHub.