Search⌘ K
AI Features

How to Draw a Radar Plot

Explore how to create radar plots by calculating angular positions, configuring polar coordinates, and plotting data with Matplotlib. Learn to visualize single or multiple data sets on radar charts for effective comparison and analysis of categorical variables.

What is radar?

The radar chart, is also known as web chart, spider chart, or spider web chart, is a chart that consists of a sequence of equi-angular spokes, called radii, where each spoke represents one of the variables that is impacting our data. The data length of a spoke is proportional to the magnitude of the variable for that data point in relation to the maximum magnitude of the variable across all data points.

The radar chart is useful in showing relative values for a single data point, or for comparing two or more items as they relate to various categories.

Unlike many of the other plot types we’ve learned about, Matplotlib doesn’t provide a radar function. In order to draw a radar chart, we need to write the code ourselves. Fortunately, drawing a radar chart is not difficult because Matplotlib supports drawing polar coordinates, and polar coordinates are the core of a radar chart.

How to draw a radar chart

In order to draw a radar chart, we follow the steps outlined below:

  • Step 1: Calculate the angle for the value of each category. We begin by assigning each category value an angle. The angles are accumulated according to the order in which they occur.
  • Step 2: Append the first item to the last, making the circular graph close.
  • Step 3: Set the coordinate system as polar for your figure.
  • Step 4: Use the plot() function to plot. We use plot() here in the same way that we used it in the lesson How to Draw a Line Plot.
  • Step 5: Fill the area. This step is optional.
values = [10, 8, 4, 5, 2, 7]
values += values[:1]
angles = [n / float(len(labels)) * 2 * math.pi for n in range(len(labels))]
angles += angles[:1]

fix, axe = plt.subplots(subplot_kw=dict(polar=True))
axe.plot(angles, values)
axe.fill(angles, values, 'skyblue', alpha=0.4)

As we can see in the example code below, from line 5 to line 8, we use the raw values to calculate the angles for each label.

At line 10, we set the graph to polar coordinates by setting subplot_kw=dict(polar=True).

At line 13 and line 14, we plot a line according to each number and its corresponding angle, and fill the area.

Python 3.5
import matplotlib.pyplot as plt
import math
labels = ["Sun", "Moon", "Jupiter", "Venus", "Mars", "Mecury"]
values = [10, 8, 4, 5, 2, 7]
values += values[:1]
angles = [n / float(len(labels)) * 2 * math.pi for n in range(len(labels))]
angles += angles[:1]
fig, axe = plt.subplots(subplot_kw=dict(polar=True), dpi=800)
axe.set_xticks(angles[:-1])
axe.set_xticklabels(labels, color='r')
axe.plot(angles, values)
axe.fill(angles, values, 'skyblue', alpha=0.4)
fig.savefig("output/output.png")
plt.close(fig)

Adding more than one radar to a figure

Comparing two data sets in one radar chart is easy. All we have to do is prepare two data sets with the same length, follow the steps described above, and plot.

As we can see in the example code below, from line 5 to line 10, two different data sets are created. The angles are also calculated based on those values.

Then, from line 15 to line 19, two different radars are plotted with two different colors.

Python 3.5
import matplotlib.pyplot as plt
import math
labels = ["Sun", "Moon", "Jupiter", "Venus", "Mars", "Mecury"]
values = [10, 8, 4, 5, 2, 7]
values2 = [3, 7, 2, 8, 5, 9]
values += values[:1]
values2 += values2[:1]
angles = [n / float(len(labels)) * 2 * math.pi for n in range(len(labels))]
angles += angles[:1]
fig, axe = plt.subplots(subplot_kw=dict(polar=True), dpi=800)
axe.set_xticks(angles[:-1])
axe.set_xticklabels(labels, color='r')
axe.plot(angles, values)
axe.fill(angles, values, 'skyblue', alpha=0.4)
axe.plot(angles, values2)
axe.fill(angles, values2, 'teal', alpha=0.4)
fig.savefig("output/output.png")
plt.close(fig)