Python - Plotting an Excel chart with pattern fills in column using XlsxWriter module

The XlsxWriter module allows you to create Excel charts with pattern fills in columns, making data visualization more distinctive and professional. Pattern fills help differentiate data series visually, especially when working with monochrome displays or printing.

Understanding Pattern Fills

Pattern fills apply textures or patterns to chart columns instead of solid colors. Common patterns include 'shingle', 'horizontal_brick', 'vertical_brick', and 'dots'. Each pattern can have foreground and background colors for customization.

Example

Let's create a column chart comparing different building materials with pattern fills −

import xlsxwriter

# Create workbook and worksheet
workbook = xlsxwriter.Workbook('chart_pattern.xlsx')
worksheet = workbook.add_worksheet()

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

# Sample data for building materials
headings = ['Shingle', 'Brick']
data = [
    [105, 150, 130, 90],   # Shingle data
    [50, 120, 100, 110],   # Brick data
]

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

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

# Add first series with shingle pattern
chart.add_series({
    'name': '=Sheet1!$A$1',
    'values': '=Sheet1!$A$2:$A$5',
    'pattern': {
        'pattern': 'shingle',
        'fg_color': '#804000',
        'bg_color': '#c68c53'
    },
    'border': {'color': '#804000'},
    'gap': 70,  # Increase gap to make patterns more visible
})

# Add second series with brick pattern
chart.add_series({
    'name': '=Sheet1!$B$1',
    'values': '=Sheet1!$B$2:$B$5',
    'pattern': {
        'pattern': 'horizontal_brick',
        'fg_color': '#b30000',
        'bg_color': '#ff6666'
    },
    'border': {'color': '#b30000'},
})

# Customize chart appearance
chart.set_title({'name': 'Building Material Usage by Region'})
chart.set_x_axis({'name': 'Region'})
chart.set_y_axis({'name': 'Number of Houses'})

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

# Save and close workbook
workbook.close()
print("Excel file with pattern-filled chart created successfully!")

Pattern Options

XlsxWriter supports various pattern types for column fills −

Pattern Type Description Best Use Case
'shingle' Diagonal overlapping pattern Roofing or covering materials
'horizontal_brick' Horizontal brick-like pattern Construction materials
'vertical_brick' Vertical brick-like pattern Wall construction data
'dots' Dotted pattern Statistical or scattered data

Key Parameters

  • pattern − Defines the pattern type (e.g., 'shingle', 'horizontal_brick')
  • fg_color − Foreground color of the pattern (hex format)
  • bg_color − Background color of the pattern (hex format)
  • border − Border color around columns for better definition
  • gap − Space between columns (increase to 70+ for pattern visibility)

Conclusion

Pattern fills in XlsxWriter charts provide excellent visual differentiation for data series. Use patterns when solid colors aren't sufficient, and always increase the gap parameter to make patterns clearly visible.

---
Updated on: 2026-03-25T09:17:09+05:30

258 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements