Help us understand the problem. What is going on with this article?

Matplotlibでレーダーチャートを描く(16行)

More than 1 year has passed since last update.

サンプル

radar.png

モチベーション

  • 公式のサンプルはうまくプロットできないし、コードが長くて死ぬ。
  • シンプルにレーダーチャートを描きたいだけなのに…。

コード

import matplotlib.pyplot as plt
import numpy as np

def plot_polar(labels, values, imgname):
    angles = np.linspace(0, 2 * np.pi, len(labels) + 1, endpoint=True)
    values = np.concatenate((values, [values[0]]))  # 閉じた多角形にする
    fig = plt.figure()
    ax = fig.add_subplot(111, polar=True)
    ax.plot(angles, values, 'o-')  # 外枠
    ax.fill(angles, values, alpha=0.25)  # 塗りつぶし
    ax.set_thetagrids(angles[:-1] * 180 / np.pi, labels)  # 軸ラベル
    ax.set_rlim(0 ,250)
    fig.savefig(imgname)
    plt.close(fig)

labels = ['HP', 'Attack', 'Defense', 'Speed']
values = [155, 156, 188, 139]
plot_polar(labels, values, "radar.png")

要約すると

  1. 極座標のAxesオブジェクトを作って、
  2. そこにポリゴンを描画する。
  3. 塗りつぶしても良し。

凡例やタイトルの追加、複数データのプロットは通常のグラフ同様にできるので省略。

既存資料

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away