DEV Community

Allen Yang
Allen Yang

Posted on

Create Radar Charts in Excel with Python

Creating and Customizing Radar Charts in Excel Using Python

Radar charts are a multidimensional data visualization tool, particularly suitable for comparing multiple variables across different dimensions. They are highly practical in scenarios such as performance evaluation, capability analysis, and product comparison. This article will demonstrate how to create radar charts and filled radar charts in Excel using Python, along with customizing chart settings.

Why Use Radar Charts

Radar charts offer the following advantages:

  • Multidimensional Comparison: Display data across multiple dimensions in a single chart
  • Intuitive Performance Analysis: Clearly show the distribution of strengths and weaknesses for each metric
  • Pattern Recognition: Quickly identify data patterns and anomalies through visual shapes
  • Ideal for Evaluation Scenarios: Such as employee performance reviews, product feature comparisons, and skill matrix analysis

Typical application scenarios include:

  • Multi-dimensional employee performance evaluation (communication skills, technical abilities, leadership, etc.)
  • Product feature comparison (price, performance, usability, reliability, etc.)
  • Market analysis (sales performance across different regions)
  • Capability model visualization (skill proficiency levels)

Environment Setup

First, install the Spire.XLS for Python library:

pip install Spire.XLS
Enter fullscreen mode Exit fullscreen mode

Spire.XLS provides a comprehensive API for Excel file operations, supporting creation, reading, modification, and conversion of Excel documents without requiring Microsoft Excel installation.

Creating a Basic Radar Chart

We will create a radar chart displaying product sales data across different regions. The sample data includes sales figures for three products (bikes, cars, trucks) in two cities (Paris and New York).

Step 1: Prepare Data and Create Worksheet

First, create an Excel workbook and add sample data:

from spire.xls import *
from spire.xls.common import *

def CreateChartData(sheet):
    # Set product titles
    sheet.Range["A1"].Value = "Product"
    sheet.Range["A2"].Value = "Bikes"
    sheet.Range["A3"].Value = "Cars"
    sheet.Range["A4"].Value = "Trucks"
    sheet.Range["A5"].Value = "Buses"

    # Set Paris sales data
    sheet.Range["B1"].Value = "Paris"
    sheet.Range["B2"].NumberValue = 4000
    sheet.Range["B3"].NumberValue = 23000
    sheet.Range["B4"].NumberValue = 4000
    sheet.Range["B5"].NumberValue = 30000

    # Set New York sales data
    sheet.Range["C1"].Value = "New York"
    sheet.Range["C2"].NumberValue = 30000
    sheet.Range["C3"].NumberValue = 7600
    sheet.Range["C4"].NumberValue = 18000
    sheet.Range["C5"].NumberValue = 8000

    # Set bold font for header row
    sheet.Range["A1:C1"].Style.Font.IsBold = True

    # Set background colors for different rows
    sheet.Range["A2:C2"].Style.KnownColor = ExcelColors.LightYellow
    sheet.Range["A3:C3"].Style.KnownColor = ExcelColors.LightGreen1
    sheet.Range["A4:C4"].Style.KnownColor = ExcelColors.LightOrange
    sheet.Range["A5:C5"].Style.KnownColor = ExcelColors.LightTurquoise

    # Set border styles
    style = sheet.Range["A1:C5"].Style
    borders = style.Borders

    # Set top border
    topborder = borders[BordersLineType.EdgeTop]
    topborder.Color = Color.FromRgb(0, 0, 128)
    borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin

    # Set bottom border
    borders[BordersLineType.EdgeBottom].Color = Color.FromRgb(0, 0, 128)
    sheet.Range["A1:C5"].Style.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin

    # Set left border
    sheet.Range["A1:C5"].Style.Borders[BordersLineType.EdgeLeft].Color = Color.FromRgb(0, 0, 128)
    sheet.Range["A1:C5"].Style.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin

    # Set right border
    sheet.Range["A1:C5"].Style.Borders[BordersLineType.EdgeRight].Color = Color.FromRgb(0, 0, 128)
    sheet.Range["A1:C5"].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin

    # Set number format to currency
    sheet.Range["B2:C5"].Style.NumberFormat = "\"$\"#,##0"

# Create workbook
workbook = Workbook()
workbook.CreateEmptySheets(1)
sheet = workbook.Worksheets[0]
sheet.Name = "Chart data"
sheet.GridLinesVisible = False

# Write chart data
CreateChartData(sheet)
Enter fullscreen mode Exit fullscreen mode

This code accomplishes the following tasks:

  • Creates a table containing product and regional sales data
  • Applies bold formatting to the header row
  • Applies different background colors to different data rows to improve readability
  • Adds dark blue borders to make the table more visually appealing
  • Formats numerical values in US dollar currency format

Step 2: Add Radar Chart and Configure Properties

Next, add a radar chart to the worksheet and set its properties:

# Add a new chart to the worksheet
chart = sheet.Charts.Add()

# Set chart position
chart.LeftColumn = 1
chart.TopRow = 6
chart.RightColumn = 11
chart.BottomRow = 29

# Set chart data range
chart.DataRange = sheet.Range["A1:C5"]
chart.SeriesDataFromRange = False

# Set chart type to radar chart
chart.ChartType = ExcelChartType.Radar

# Set chart title
chart.ChartTitle = "Sale market by region"
chart.ChartTitleArea.IsBold = True
chart.ChartTitleArea.Size = 12

# Hide plot area background
chart.PlotArea.Fill.Visible = False

# Set legend position
chart.Legend.Position = LegendPositionType.Corner

# Save document
workbook.SaveToFile("CreateRadarChart.xlsx", ExcelVersion.Version2013)
workbook.Dispose()
Enter fullscreen mode Exit fullscreen mode

Result preview:

Python Create Excel Radar Chart

Key API explanations:

  • sheet.Charts.Add(): Adds a new chart object to the worksheet
  • chart.DataRange: Specifies the data range used by the chart, using the A1:C5 range here
  • chart.SeriesDataFromRange = False: Indicates that data series are organized by columns rather than rows
  • ExcelChartType.Radar: Sets the chart type to radar chart (line type)
  • chart.ChartTitle: Sets the chart title text
  • chart.PlotArea.Fill.Visible = False: Hides the plot area background for a cleaner chart appearance
  • LegendPositionType.Corner: Places the legend in the corner position

The generated radar chart displays sales data for each product in both regions using lines, facilitating comparative analysis.

Creating Filled Radar Charts

In addition to standard radar charts, you can also create filled radar charts. This type fills the area between data points with color, creating a more striking visual effect.

# Create workbook
workbook = Workbook()
workbook.CreateEmptySheets(1)
sheet = workbook.Worksheets[0]
sheet.Name = "Chart data"
sheet.GridLinesVisible = False

# Write chart data
CreateChartData(sheet)

# Add a new chart to the worksheet
chart = sheet.Charts.Add()

# Set chart position
chart.LeftColumn = 1
chart.TopRow = 6
chart.RightColumn = 11
chart.BottomRow = 29

# Set chart data range
chart.DataRange = sheet.Range["A1:C5"]
chart.SeriesDataFromRange = False

# Set chart type to filled radar chart
chart.ChartType = ExcelChartType.RadarFilled

# Set chart title
chart.ChartTitle = "Sale market by region"
chart.ChartTitleArea.IsBold = True
chart.ChartTitleArea.Size = 12

# Hide plot area background
chart.PlotArea.Fill.Visible = False

# Set legend position
chart.Legend.Position = LegendPositionType.Corner

# Save document
workbook.SaveToFile("CreateRadarChart_Fill.xlsx", ExcelVersion.Version2013)
workbook.Dispose()
Enter fullscreen mode Exit fullscreen mode

Result preview:

Python Create Excel Filled Radar Chart

The main differences between filled radar charts and standard radar charts are:

  • ExcelChartType.RadarFilled: Uses the filled type, where areas between data points are filled with color
  • More suitable for emphasizing data ranges and area comparisons
  • Visually more prominent, ideal for presentations and reports

Customizing Radar Chart Styles

After creating a basic radar chart, you can further customize various chart elements to meet specific presentation requirements.

Adjusting Data Series Colors

You can set different colors for different data series to improve chart distinguishability:

# Get the first data series and set color
series = chart.Series[0]
series.Format.LineProperties.Color = Color.FromRgb(255, 0, 0)  # Red
series.Format.LineProperties.Width = 2.5

# Get the second data series and set color
series2 = chart.Series[1]
series2.Format.LineProperties.Color = Color.FromRgb(0, 0, 255)  # Blue
series2.Format.LineProperties.Width = 2.5
Enter fullscreen mode Exit fullscreen mode

Customizing Axes

You can adjust radar chart axis properties, including scales and label formatting:

# Get the value axis
valueAxis = chart.PrimaryValueAxis

# Set axis title
valueAxis.Title = "Sales Amount"
valueAxis.TitleArea.IsBold = True
valueAxis.TitleArea.Size = 10

# Set axis number format
valueAxis.NumberFormat = "$#,##0"

# Set major gridline style
valueAxis.MajorGridLines.LineProperties.Color = Color.FromRgb(200, 200, 200)
valueAxis.MajorGridLines.LineProperties.DashStyle = LineDashStyleType.Dash
Enter fullscreen mode Exit fullscreen mode

Adding Data Labels

Adding labels to data points can display specific values more clearly:

# Add data labels for each series
for series in chart.Series:
    seriePoints = series.DataPoints
    for point in seriePoints:
        point.DataLabels.HasValue = True
        point.DataLabels.Position = DataLabelPositionType.Above
Enter fullscreen mode Exit fullscreen mode

Adjusting Chart Size and Position

You can adjust the chart's position and size on the worksheet as needed:

# Set the column and row range occupied by the chart
chart.LeftColumn = 1      # Starting column
chart.TopRow = 6          # Starting row
chart.RightColumn = 11    # Ending column
chart.BottomRow = 29      # Ending row

# Or use pixel-level precise control
chart.Top = 100           # Top position (pixels)
chart.Left = 50           # Left position (pixels)
chart.Width = 500         # Width (pixels)
chart.Height = 400        # Height (pixels)
Enter fullscreen mode Exit fullscreen mode

Practical Application Recommendations

When using radar charts in actual projects, consider the following best practices:

Data Preparation

  • Ensure all dimensions use the same unit of measurement or are normalized
  • Keep the number of data dimensions manageable, preferably within 5-8 dimensions
  • Provide clear label descriptions for each dimension

Visual Optimization

  • Use contrasting colors to distinguish different data series
  • Appropriately adjust line width and transparency to avoid difficulty in identification when overlapping
  • Pay attention to data label positioning to avoid occlusion

Interaction Enhancement

  • In web applications, combine with JavaScript libraries to implement interactive radar charts
  • Add hover tooltips to display detailed values
  • Support dynamic toggling to show/hide specific data series

Applicable Scenarios

  • Performance evaluation systems: Display employee performance across multiple assessment dimensions
  • Product comparison tools: Compare various metrics of different products
  • Skill matrices: Visualize team members' skill distributions
  • Competitive analysis: Compare enterprise metrics against competitors

Summary

This article demonstrated how to create and customize radar charts in Excel using Python. With the Spire.XLS library, we can:

  • Easily create standard radar charts and filled radar charts
  • Customize chart properties such as colors, positions, and titles
  • Adjust the styling of axes, legends, and data labels
  • Apply radar charts to various data analysis and visualization scenarios

Radar charts are a powerful tool for multidimensional data analysis, especially suitable for scenarios requiring simultaneous comparison of multiple variables. Combined with Python's automation capabilities, you can batch generate radar chart reports, significantly improving data analysis efficiency.

By flexibly utilizing these APIs, you can create professional and aesthetically pleasing radar charts tailored to your specific needs, providing strong support for data analysis and decision-making.

Top comments (0)