Python - Plotting an Excel chart with Gradient fills using XlsxWriter module

XlsxWriter is a Python library for creating Excel files with advanced formatting, including charts with gradient fills. This tutorial shows how to create a column chart with gradient-filled data series.

Installing XlsxWriter

First, install the required module ?

pip install xlsxwriter

Creating Excel Chart with Gradient Fills

The following example creates a column chart with two data series, each having different gradient colors ?

import xlsxwriter

# Create a workbook and add a worksheet
workbook = xlsxwriter.Workbook('chart_gradient1.xlsx')
worksheet = workbook.add_worksheet()

# Create a bold format for headers
bold = workbook.add_format({'bold': 1})

# Add the worksheet data that the charts will refer to
headings = ['Number', 'Batch 1', 'Batch 2']
data = [
    [2, 3, 4, 5, 6, 7],
    [10, 40, 50, 20, 10, 50],
    [30, 60, 70, 50, 40, 30],
]

# Write headers and data to worksheet
worksheet.write_row('A1', headings, bold)
worksheet.write_column('A2', data[0])
worksheet.write_column('B2', data[1])
worksheet.write_column('C2', data[2])

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

# Add first series with gradient fill
chart.add_series({
    'name':       '=Sheet1!$B$1',
    'categories': '=Sheet1!$A$2:$A$7',
    'values':     '=Sheet1!$B$2:$B$7',
    'gradient':   {'colors': ['#963735', '#F1DCDB']}
})

# Add second series with different gradient fill
chart.add_series({
    'name':       '=Sheet1!$C$1',
    'categories': '=Sheet1!$A$2:$A$7',
    'values':     '=Sheet1!$C$2:$C$7',
    'gradient':   {'colors': ['#E36C0A', '#FCEADA']}
})

# Configure chart appearance
chart.set_title({'name': 'Chart With Gradient Fills'})
chart.set_x_axis({'name': 'Test number'})
chart.set_y_axis({'name': 'Sample length (mm)'})
chart.set_legend({'none': True})

# Insert chart into worksheet
worksheet.insert_chart('D2', chart, {'x_offset': 25, 'y_offset': 10})

# Close the workbook
workbook.close()

Key Components

Gradient Configuration

The gradient parameter accepts a dictionary with color specifications ?

# Gradient from dark red to light pink
gradient_config = {'colors': ['#963735', '#F1DCDB']}

# Gradient from dark orange to light cream
gradient_config = {'colors': ['#E36C0A', '#FCEADA']}

Chart Series Parameters

Parameter Description Example
name Series name (legend label) '=Sheet1!$B$1'
categories X-axis data range '=Sheet1!$A$2:$A$7'
values Y-axis data range '=Sheet1!$B$2:$B$7'
gradient Gradient fill colors {'colors': ['#963735', '#F1DCDB']}

Output

The code creates an Excel file named chart_gradient1.xlsx containing a column chart with gradient-filled bars. Each data series displays with smooth color transitions from dark to light tones.

Conclusion

XlsxWriter's gradient feature enhances chart visualization by adding smooth color transitions to data series. Use the gradient parameter with color arrays to create visually appealing Excel charts with professional styling.

Updated on: 2026-03-25T09:16:47+05:30

296 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements