分享

Python matplotlib 画雷达图

 勤悦轩 2015-09-14

import numpy as np

import pylab as pl


class Radar(object):


    def __init__(self, fig, titles, labels, rect=None):

        if rect is None:

            rect = [0.05, 0.05, 0.95, 0.95]


        self.n = len(titles)

        self.angles = np.arange(90, 90+360, 360.0/self.n)

        self.axes = [fig.add_axes(rect, projection="polar", label="axes%d" % i) 

                         for i in range(self.n)]


        self.ax = self.axes[0]

        self.ax.set_thetagrids(angles, labels=titles, fontsize=14)


        for ax in self.axes[1:]:

            ax.patch.set_visible(False)

            ax.grid("off")

            ax.xaxis.set_visible(False)


        for ax, angle, label in zip(self.axes, self.angles, labels):

            ax.set_rgrids(range(1, 6), angle=angle, labels=label)

            ax.spines["polar"].set_visible(False)

            ax.set_ylim(0, 5)


    def plot(self, values, *args, **kw):

        angle = np.deg2rad(np.r_[self.angles, self.angles[0]])

        values = np.r_[values, values[0]]

        self.ax.plot(angle, values, *args, **kw)



titles = list("ABCDE")


labels = [

    list("abcde"), list("12345"), list("uvwxy"), 

    ["one", "two", "three", "four", "five"],

    list("jklmn")

]


fig = pl.figure(figsize=(6, 6))


titles = list("ABCDE")


labels = [

    list("abcde"), list("12345"), list("uvwxy"), 

    ["one", "two", "three", "four", "five"],

    list("jklmn")

]


radar = Radar(fig, titles, labels)

radar.plot([1, 3, 2, 5, 4],  "-", lw=2, color="b", alpha=0.4, label="first")

radar.plot([2.3, 2, 3, 3, 2],"-", lw=2, color="r", alpha=0.4, label="second")

radar.plot([3, 4, 3, 4, 2], "-", lw=2, color="g", alpha=0.4, label="third")

radar.ax.legend()

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多