Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python - Plotting Radar charts in excel sheet using XlsxWriter module
A radar chart is a graphical method of displaying multivariate data in the form of a two-dimensional chart where three or more quantitative variables are represented on axes starting from the same point. Python's XlsxWriter module allows you to create radar charts directly in Excel files.
Installing XlsxWriter
First, install the XlsxWriter module if you haven't already ?
pip install XlsxWriter
Creating a Radar Chart
Here's how to create a radar chart with sample data comparing two batches ?
import xlsxwriter
# Create a workbook and add a worksheet
workbook = xlsxwriter.Workbook('chart_radar1.xlsx')
worksheet = workbook.add_worksheet()
# Create a bold format for headings
bold = workbook.add_format({'bold': 1})
# Define headings and data
headings = ['Number', 'Batch 1', 'Batch 2']
data = [
[2, 3, 4, 5, 6, 7],
[80, 80, 100, 60, 50, 100],
[60, 50, 60, 20, 10, 20],
]
# Write headings with bold format
worksheet.write_row('A1', headings, bold)
# Write data columns
worksheet.write_column('A2', data[0])
worksheet.write_column('B2', data[1])
worksheet.write_column('C2', data[2])
# Create a radar chart
chart1 = workbook.add_chart({'type': 'radar'})
# Add first data series (Batch 1)
chart1.add_series({
'name': '=Sheet1!$B$1',
'categories': '=Sheet1!$A$2:$A$7',
'values': '=Sheet1!$B$2:$B$7',
})
# Add second data series (Batch 2)
chart1.add_series({
'name': ['Sheet1', 0, 2],
'categories': ['Sheet1', 1, 0, 6, 0],
'values': ['Sheet1', 1, 2, 6, 2],
})
# Configure chart appearance
chart1.set_title({'name': 'Results of Data Analysis'})
chart1.set_x_axis({'name': 'Test Number'})
chart1.set_y_axis({'name': 'Data Length (mm)'})
chart1.set_style(11)
# Insert chart into worksheet
worksheet.insert_chart('E2', chart1)
# Close the workbook
workbook.close()
print("Radar chart created successfully in 'chart_radar1.xlsx'")
Radar chart created successfully in 'chart_radar1.xlsx'
Key Components
Chart Configuration
The radar chart is configured with these main components ?
-
Chart Type:
{'type': 'radar'}creates the radar chart -
Data Series: Each
add_series()call adds a line to the radar - Categories: Define the axis labels (Test Numbers 2-7)
- Values: Define the data points for each series
Range Reference Formats
XlsxWriter supports two ways to reference cell ranges ?
| Format | Example | Description |
|---|---|---|
| Excel Style | '=Sheet1!$A$2:$A$7' |
Traditional Excel range notation |
| List Style | ['Sheet1', 1, 0, 6, 0] |
[sheet, first_row, first_col, last_row, last_col] |
Chart Customization
You can customize the radar chart appearance with additional options ?
import xlsxwriter
workbook = xlsxwriter.Workbook('custom_radar.xlsx')
worksheet = workbook.add_worksheet()
# Sample data
headings = ['Metric', 'Team A', 'Team B']
data = [
['Speed', 'Accuracy', 'Efficiency', 'Quality', 'Innovation'],
[85, 92, 78, 88, 95],
[78, 88, 85, 92, 82],
]
# Write data
worksheet.write_row('A1', headings, workbook.add_format({'bold': True}))
for i, row in enumerate(data):
worksheet.write_column(chr(65+i) + '2', row)
# Create radar chart with custom formatting
chart = workbook.add_chart({'type': 'radar'})
chart.add_series({
'name': '=Sheet1!$B$1',
'categories': '=Sheet1!$A$2:$A$6',
'values': '=Sheet1!$B$2:$B$6',
'line': {'color': 'blue', 'width': 2},
'marker': {'type': 'circle', 'size': 6, 'fill': {'color': 'blue'}}
})
chart.add_series({
'name': '=Sheet1!$C$1',
'categories': '=Sheet1!$A$2:$A$6',
'values': '=Sheet1!$C$2:$C$6',
'line': {'color': 'red', 'width': 2},
'marker': {'type': 'square', 'size': 6, 'fill': {'color': 'red'}}
})
chart.set_title({'name': 'Team Performance Comparison'})
chart.set_legend({'position': 'bottom'})
worksheet.insert_chart('E2', chart)
workbook.close()
print("Custom radar chart created successfully!")
Custom radar chart created successfully!
Conclusion
XlsxWriter makes it easy to create radar charts in Excel files using Python. The module supports both Excel-style and list-style range references, and offers extensive customization options for colors, markers, and chart styling.
