How to create a Radar Chart on Python in 3 screenshots

Elad Gvirtz
2 min readJul 17, 2021
  • Radar charts are a way to visualize multivariate data.
  • Giving an axis for each variable, and these axes are arranged radially around a central point and spaced equally.
  • The data from a single observation are plotted along each axis and connected to form a polygon. Multiple observations can be placed in a single chart by displaying multiple polygons, overlaying them, and reducing the opacity of each polygon.

Screenshot 1 — That your data:

## reference ###  https://python-graph-gallery.com/391-radar-chart-with-several-individuals/# https://www.kaggle.com/typewind/draw-a-radar-chart-with-python-in-a-simple-wayimport matplotlib.pyplot as plt
import pandas as pd
from math import pi
# Set data
df = pd.DataFrame({
'name': ['John','David'],
'Neuroticism': [5, 1],
'Extraversion': [2, 5],
'Agreeableness': [3, 2],
'Conscientiousness': [3, 3],
'Openness to experience': [5, 1]})

Screenshot 2 — That your actions:

# number of variablecategories=list(df)[1:]
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(111, polar=True)
# If you want the first axis to be on top:
ax.set_theta_offset(pi / 2)
ax.set_theta_direction(-1)
# Draw one axe per variable + add labels labels yet
plt.xticks(angles[:-1], categories)
# Draw ylabels
ax.set_rlabel_position(0)
plt.yticks([1,2,3,4], ["1","2","3","4"], color="grey", size=7)
plt.ylim(0,5)
# ------- PART 2: Add plots
# Name1
values=df.loc[0].drop('name').values.flatten().tolist()
values += values[:1]
ax.plot(angles, values, linewidth=1, linestyle='solid', label="John")
ax.fill(angles, values, 'b', alpha=0.1)
# Name2
values=df.loc[1].drop('name').values.flatten().tolist()
values += values[:1]
ax.plot(angles, values, linewidth=1, linestyle='solid', label="David")
ax.fill(angles, values, 'r', alpha=0.1)
# Add legend
plt.legend(loc='upper right', bbox_to_anchor=(0.1, 0.1))

Screenshot 3— Try another Radar Chart:

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
labels=np.array(['Neuroticism', 'Extraversion', 'Agreeableness', 'Conscientiousness', 'Openness to experience'])
stats=np.array([5, 2, 3, 3, 2])
angles=np.linspace(0, 2*np.pi, len(labels), endpoint=False)
# close the plot
stats=np.concatenate((stats,[stats[0]]))
angles=np.concatenate((angles,[angles[0]]))
fig=plt.figure()
ax = fig.add_subplot(111, polar=True)
ax.plot(angles, stats, 'o-', linewidth=2)
ax.fill(angles, stats, alpha=0.3)
ax.set_thetagrids(angles * 180/np.pi, labels)
ax.grid(True)

For more visualization and analytics
https://drivenn.io/

--

--