Fill_Between不正确

时间:2016-06-05 00:57:01

标签: python matplotlib

如果没有有问题的代码,图像几乎是正确的,我得到: enter image description here

现在您将看到我想在第5行和第5行中填写5.67和7.33中的最后一个圆圈。但是当我插入与其他两个相同的代码时,它看起来不正确 enter image description here 它填补了网格的错误部分。

我尝试过使用这些数字,但我不确定为什么会这样做。我添加了我的代码,有没有人知道它为什么这样做,所以我可以尝试修复它。

import matplotlib.pyplot as plt
import numpy as np
import Image

def _invert(x, limits):
    """inverts a value x on a scale from
    limits[0] to limits[1]"""
    return limits[1] - (x - limits[0])

def _scale_data(data, ranges):
    """scales data[1:] to ranges[0],
    inverts if the scale is reversed"""
    for d, (y1, y2) in zip(data[1:], ranges[1:]):
        assert (y1 <= d <= y2) or (y2 <= d <= y1)
    x1, x2 = ranges[0]
    d = data[0]
    if x1 > x2:
        d = _invert(d, (x1, x2))
        x1, x2 = x2, x1
    sdata = [d]
    for d, (y1, y2) in zip(data[1:], ranges[1:]):
        if y1 > y2:
            d = _invert(d, (y1, y2))
            y1, y2 = y2, y1
        sdata.append((d-y1) / (y2-y1) 
                     * (x2 - x1) + x1)
    return sdata

class ComplexRadar():
    def __init__(self, fig, variables, ranges,
                 n_ordinate_levels=7):
        angles = np.arange(0, 360, 360./len(variables))

        axes = [fig.add_axes([0.1,0.1,0.9,0.9],polar=True, axisbg='#ffffff',
                label = "axes{}".format(i))
                for i in range(len(variables))]
        l, text = axes[0].set_thetagrids(angles, 
                                         labels=variables, weight='semibold')
        [txt.set_rotation(angle-90) for txt, angle 
             in zip(text, angles)]
        for ax in axes[1:]:
            ax.patch.set_visible(False)
            ax.grid("off")
            ax.xaxis.set_visible(False)

        for i, ax in enumerate(axes):
            grid = np.linspace(*ranges[i], 
                               num=n_ordinate_levels)
            gridlabel = ["{}".format(round(x,2)) 
                         for x in grid]
            if ranges[i][0] > ranges[i][1]:
                grid = grid[::-1]
            gridlabel[0] = ""
            ax.set_rgrids(grid, labels=gridlabel, ha="center", va="center",
                         angle=angles[i], weight='semibold', fontsize=8, color='#333333')
            ax.spines["polar"].set_visible(False)
            theta = np.linspace(0., 2*np.pi, 80, endpoint=True)
            # THE LINE BELOW IS CAUSING THE PROBLEMS
            ax.fill_between(theta, 5.67, 7.33, color="#666666", alpha=0.1, linewidth=0)
            ax.fill_between(theta, 9., 10.67, color="#666666", alpha=0.1, linewidth=0)
            ax.fill_between(theta, 12.33, 14., color="#666666", alpha=0.1, linewidth=0)
            ax.set_ylim(*ranges[i])
        # variables for plotting
        self.angle = np.deg2rad(np.r_[angles, angles[0]])
        self.ranges = ranges
        self.ax = axes[0]
    def plot(self, data, *args, **kw):
        sdata = _scale_data(data, self.ranges)
        self.ax.plot(self.angle, np.r_[sdata, sdata[0]], *args, **kw)
    def fill(self, data, *args, **kw):
        sdata = _scale_data(data, self.ranges)
        self.ax.fill(self.angle, np.r_[sdata, sdata[0]], *args, **kw)


variables = ("1", "2", "3", "4", "5", "6", "7", "8")
data = (25.51, 1.45, 0.08, 0.2, 9.08, 58.4, 0.47, 6.63)
ranges = [(20, 30), (1, 3), (0.01, 0.75), (0.01, 0.5), (4, 14), (50, 85), (0.3, 1), (4.5, 7)]

# plotting
fig1 = plt.figure(figsize=(10, 10))
radar = ComplexRadar(fig1, variables, ranges)
radar.plot(data, color='#0000e6', linewidth=1)
radar.fill(data, alpha=0.4, color='#0000e6')
plt.savefig('radar-chart2.png',orientation='landscape',bbox_inches='tight',pad_inches=.8)  

1 个答案:

答案 0 :(得分:0)

您的fill_between工作正常。但你认为这是错误的。问题发生在__init__的轴循环中。您有多个轴并尝试为每个轴应用fill_between

对于第一个和第二个fill_between,此方法产生正确的填充,因为它们的限制只有一个轴(我猜是4号)。

如果您将fill_between移到周期之外并写成:

axes[4].fill_between(theta, 5.67, 7.33, color="#666666", alpha=0.1, linewidth=0)
axes[4].fill_between(theta, 9., 10.67, color="#666666", alpha=0.1, linewidth=0)
axes[4].fill_between(theta, 12.33, 14., color="#666666", alpha=0.1, linewidth=0)

您将获得下一张图片:

enter image description here