Fundamentals 4 min read

How to Plot a Radar Chart for Cluster Centers Using Python and Matplotlib

This tutorial demonstrates how to create a radar chart in Python with Matplotlib to visualize cluster center values, providing the necessary data preparation steps, complete plotting code, and example output images for an airline customer value analysis.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
How to Plot a Radar Chart for Cluster Centers Using Python and Matplotlib

The author previously performed an airline customer value analysis and needed a radar chart to display cluster centroids, but the required code was missing.

The data object, named data_cluster , contains the cluster name and the centroid values (ZL, ZR, ZF, ZM, ZC) for each cluster.

The following Python code uses numpy , pandas , and matplotlib to generate the radar chart:

import numpy as np
from matplotlib.pyplot import plt

def plot_radar(data):
    '''
    the first column of the data is the cluster name;
    the second column is the number of each cluster;
    the last are those to describe the center of each cluster.
    '''
    kinds = data.iloc[:, 0]
    labels = data.iloc[:, 2:].columns
    centers = pd.concat([data.iloc[:, 2:], data.iloc[:,2]], axis=1)
    centers = np.array(centers)
    n = len(labels)
    angles = np.linspace(0, 2*np.pi, n, endpoint=False)
    angles = np.concatenate((angles, [angles[0]]))

    fig = plt.figure()
    ax = fig.add_subplot(111, polar=True)  # polar coordinates

    floor = np.floor(centers.min())
    ceil = np.ceil(centers.max())
    for i in np.arange(floor, ceil + 0.5, 0.5):
        ax.plot(angles, [i] * (n + 1), '--', lw=0.5, color='black')

    for i in range(n):
        ax.plot([angles[i], angles[i]], [floor, ceil], '--', lw=0.5, color='black')

    for i in range(len(kinds)):
        ax.plot(angles, centers[i], lw=2, label=kinds[i])
        # ax.fill(angles, centers[i])

    ax.set_thetagrids(angles * 180 / np.pi, labels)
    plt.legend(loc='lower right', bbox_to_anchor=(1.5, 0.0))

    ax.set_theta_zero_location('N')
    ax.spines['polar'].set_visible(False)
    ax.grid(False)
    ax.set_yticks([])

    plt.show()

Calling plot_radar(data_cluster) displays the radar chart for the clusters.

The unstyled radar chart is shown, and optional lines that modify the polar axis (such as hiding the outer circle or grid) are provided as commented code.

An example of the resulting chart image is included to illustrate the visual output.

pythonclusteringMatplotlibRadar Chart
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.