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
Selected Reading
How to make a polygon radar (spider) chart in Python Matplotlib?
A radar chart (also called spider chart) displays multivariate data in a circular format. Each variable is represented on a separate axis radiating from the center, making it ideal for comparing performance across multiple categories.
Basic Radar Chart
Create a simple radar chart using polar coordinates ?
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# Set figure size
plt.rcParams["figure.figsize"] = [8.0, 8.0]
plt.rcParams["figure.autolayout"] = True
# Create sample data
df = pd.DataFrame({
'sports': ['Strength', 'Speed', 'Power', 'Agility', 'Endurance', 'Analytical'],
'values': [7, 8, 6, 10, 8, 9]
})
# Create polar subplot
fig = plt.figure()
ax = fig.add_subplot(111, projection="polar")
# Calculate angles for each category
theta = np.arange(len(df) + 1) / float(len(df)) * 2 * np.pi
# Get values and close the polygon
values = df['values'].values
values = np.append(values, values[0])
# Plot the radar chart
ax.plot(theta, values, color="purple", marker="o", linewidth=2, label="Performance")
ax.fill(theta, values, 'green', alpha=0.3)
# Add category labels
ax.set_xticks(theta[:-1])
ax.set_xticklabels(df['sports'])
# Set radial limits
ax.set_ylim(0, 12)
plt.title("Performance Radar Chart", pad=20)
plt.legend(loc='upper right', bbox_to_anchor=(1.2, 1.1))
plt.show()
Multiple Series Radar Chart
Compare multiple datasets on the same radar chart ?
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# Create data for two players
categories = ['Strength', 'Speed', 'Power', 'Agility', 'Endurance', 'Analytical']
player1_values = [7, 8, 6, 10, 8, 9]
player2_values = [9, 6, 8, 7, 9, 7]
# Setup the radar chart
fig, ax = plt.subplots(figsize=(8, 8), subplot_kw=dict(projection="polar"))
# Calculate angles
theta = np.linspace(0, 2 * np.pi, len(categories), endpoint=False)
theta = np.concatenate((theta, [theta[0]])) # Close the plot
# Close the data
player1_values += [player1_values[0]]
player2_values += [player2_values[0]]
# Plot both series
ax.plot(theta, player1_values, 'o-', linewidth=2, label='Player 1', color='blue')
ax.fill(theta, player1_values, alpha=0.25, color='blue')
ax.plot(theta, player2_values, 'o-', linewidth=2, label='Player 2', color='red')
ax.fill(theta, player2_values, alpha=0.25, color='red')
# Add labels
ax.set_xticks(theta[:-1])
ax.set_xticklabels(categories)
ax.set_ylim(0, 12)
# Add grid and legend
ax.grid(True)
plt.legend(loc='upper right', bbox_to_anchor=(1.3, 1.1))
plt.title("Player Comparison", pad=20)
plt.show()
Customized Radar Chart
Add custom styling and annotations ?
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# Sample data
categories = ['Communication', 'Problem Solving', 'Leadership', 'Creativity', 'Technical Skills']
scores = [8.5, 7.2, 9.1, 6.8, 8.9]
# Setup
fig, ax = plt.subplots(figsize=(10, 8), subplot_kw=dict(projection="polar"))
# Calculate angles
theta = np.linspace(0, 2 * np.pi, len(categories), endpoint=False)
theta = np.concatenate((theta, [theta[0]]))
scores_closed = scores + [scores[0]]
# Create the plot
ax.plot(theta, scores_closed, 'o-', linewidth=3, color='darkblue', markersize=8)
ax.fill(theta, scores_closed, alpha=0.4, color='lightblue')
# Customize
ax.set_xticks(theta[:-1])
ax.set_xticklabels(categories, fontsize=12)
ax.set_ylim(0, 10)
ax.set_yticks([2, 4, 6, 8, 10])
ax.set_yticklabels(['2', '4', '6', '8', '10'], fontsize=10)
# Add value labels
for angle, value, label in zip(theta[:-1], scores, categories):
ax.text(angle, value + 0.5, f'{value}', ha='center', va='center', fontsize=10, fontweight='bold')
ax.grid(True, alpha=0.3)
plt.title("Skills Assessment Radar Chart", size=16, fontweight='bold', pad=30)
plt.show()
Key Components
| Component | Purpose | Parameter |
|---|---|---|
projection="polar" |
Creates polar coordinate system | Essential for radar charts |
theta |
Angle positions for categories | Usually evenly spaced |
ax.fill() |
Fills area inside polygon | Use alpha for transparency |
set_ylim() |
Sets radial axis limits | Usually starts from 0 |
Conclusion
Radar charts effectively visualize multivariate data by plotting variables on radial axes. Use projection="polar" to create the circular layout and fill() to highlight the data area for better visual impact.
Advertisements
