#392 Use faceting for Radar chart

The charts #390 and #391 show how to make a radar chart with one or few individuals. However, it is not advised to display to many entities on the same spider plot: it would result in an unreadable plot. Instead, prefer faceting. When you display each individual in a separate plot, the figure stays clear and the reader can compare the shape of each plot.

Here is the code allowing to make this figure. Do not hesitate to read this page to understand how faceting works with Matplotlib.

# Libraries
import matplotlib.pyplot as plt
import pandas as pd
from math import pi

# Set data
df = pd.DataFrame({
'group': ['A','B','C','D'],
'var1': [38, 1.5, 30, 4],
'var2': [29, 10, 9, 34],
'var3': [8, 39, 23, 24],
'var4': [7, 31, 33, 14],
'var5': [28, 15, 32, 14]

# ------- PART 1: Define a function that do a plot for one line of the dataset!

def make_spider( row, title, color):

# number of variable
N = len(categories)

# What will be the angle of each axis in the plot? (we divide the plot / number of variable)
angles = [n / float(N) * 2 * pi for n in range(N)]
angles += angles[:1]

# Initialise the spider plot
ax = plt.subplot(2,2,row+1, polar=True, )

# If you want the first axis to be on top:
ax.set_theta_offset(pi / 2)

# Draw one axe per variable + add labels labels yet
plt.xticks(angles[:-1], categories, color='grey', size=8)

# Draw ylabels
plt.yticks([10,20,30], ["10","20","30"], color="grey", size=7)

# Ind1
values += values[:1]
ax.plot(angles, values, color=color, linewidth=2, linestyle='solid')
ax.fill(angles, values, color=color, alpha=0.4)

# Add a title
plt.title(title, size=11, color=color, y=1.1)

# ------- PART 2: Apply to all individuals
# initialize the figure
plt.figure(figsize=(1000/my_dpi, 1000/my_dpi), dpi=my_dpi)

# Create a color palette:
my_palette = plt.cm.get_cmap("Set2", len(df.index))

# Loop to plot
for row in range(0, len(df.index)):
make_spider( row=row, title='group '+df['group'][row], color=my_palette(row))

Leave a Reply

Your email address will not be published.