雷达图不以matplotlib为中心

时间:2016-08-13 20:16:38

标签: python matplotlib radar-chart

我试图了解雷达图在matplotlib中是如何工作的。我使用this线程中的代码,但我生成的图不是正确居中的,并且缺少轴。我'我们尝试使用matplotlib 1.3.1,1.4.1和1.5.1以防万一在最新版本中发生了变化。

enter image description here

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(self.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)



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()

1 个答案:

答案 0 :(得分:4)

我遇到了同样的问题,但我发现问题出在以下部分:

time

所以将其改为

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

并使用self.angles = np.arange(0, 360, 360.0/self.n)轮换每个ax

修改后的类看起来像这样:

ax.set_theta_offset(np.deg2rad(90))

注意:我已将参数# Python 3.4 # matplotlib 1.5.3 class Radar(object): def __init__(self, fig, titles, labels, rotation=0, rect=None): if rect is None: rect = [0.05, 0.05, 0.95, 0.95] self.n = len(titles) self.angles = np.arange(0, 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(self.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, 6) ax.set_theta_offset(np.deg2rad(rotation)) 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) 添加到rotation=0,并将轮播应用到最后一个循环中的所有__init__。< / p>

我知道问题被问到已经有一段时间了,但我认为其他人会偶然发现这个问题。