热线电话:13121318867

登录
2021-01-24 阅读量: 2141
用python画雷达图的时候出现错误提示
for i, v in enumerate(center_num):
    # 设置雷达图的角度,用于平分切开一个圆面
    angles = np.linspace(0, 2 * np.pi, N, endpoint=False)
    # 为了使雷达图一圈封闭起来,需要下面的步骤
    center = np.concatenate((v[:], [v[0]]))
    angles = np.concatenate((angles, [angles[0]]))
    print(v)
    print(center)
    print(angles)
    # 绘制折线图
    ax.plot(angles, center, 'o-', linewidth=2, label="第%d簇人群,%d人" % (i + 1, num_kmeans[i]))
    # 填充颜色
    ax.fill(angles, center, alpha=0.25)
    # 添加每个特征的标签
    ax.set_thetagrids(angles * 180 / np.pi, feature, fontsize=15)
    # 设置雷达图的范围
    ax.set_ylim(min_ - 0.1, max_ + 0.1)
    # 添加标题
    plt.title('客户群特征分析图', fontsize=20)
    # 添加网格线
    ax.grid(True)
    # 设置图例
    plt.legend(loc='upper right', bbox_to_anchor=(1.3, 1.0), ncol=1, fancybox=True, shadow=True)
# 显示图形
plt.show()


错误提示如下:

[-0.83652059  0.01038426 -0.02660641]
[-0.83652059  0.01038426 -0.02660641 -0.83652059]
[0.        2.0943951 4.1887902 0.       ]
---------------------------------------------------------------------------ValueError                                Traceback (most recent call last)<ipython-input-32-247e9bb892c6> in <module>     13     ax.fill(angles, center, alpha=0.25)     14     # 添加每个特征的标签---> 15     ax.set_thetagrids(angles * 180 / np.pi, feature, fontsize=15)     16     # 设置雷达图的范围     17     ax.set_ylim(min_ - 0.1, max_ + 0.1)D:\ProgramData\Anaconda3\lib\site-packages\matplotlib\projections\polar.py in set_thetagrids(self, angles, labels, fmt, **kwargs)   1344         self.set_xticks(angles)   1345         if labels is not None:-> 1346             self.set_xticklabels(labels)   1347         elif fmt is not None:   1348             self.xaxis.set_major_formatter(mticker.FormatStrFormatter(fmt))D:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in wrapper(self, *args, **kwargs)     61      62         def wrapper(self, *args, **kwargs):---> 63             return get_method(self)(*args, **kwargs)     64      65         wrapper.__module__ = owner.__module__D:\ProgramData\Anaconda3\lib\site-packages\matplotlib\cbook\deprecation.py in wrapper(*args, **kwargs)    449                 "parameter will become keyword-only %(removal)s.",    450                 name=name, obj_type=f"parameter of {func.__name__}()")--> 451         return func(*args, **kwargs)    452     453     return wrapperD:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axis.py in _set_ticklabels(self, labels, fontdict, minor, **kwargs)   1791         if fontdict is not None:   1792             kwargs.update(fontdict)-> 1793         return self.set_ticklabels(labels, minor=minor, **kwargs)   1794    1795     @cbook._make_keyword_only("3.2", "minor")D:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axis.py in set_ticklabels(self, ticklabels, minor, **kwargs)   1712             # remove all tick labels, so only error for > 0 ticklabels   1713             if len(locator.locs) != len(ticklabels) and len(ticklabels) != 0:-> 1714                 raise ValueError(   1715                     "The number of FixedLocator locations"   1716                     f" ({len(locator.locs)}), usually from a call to"ValueError: The number of FixedLocator locations (4), usually from a call to set_ticks, does not match the number of ticklabels (3).


image.png

image.png

根据错误提示应该是标签位置和标签个数不对应

image.png

可以看到两个数组的元素个数确实不一样,找到了直接原因,我们看下代码

代码的循环体进行如下修改,使得angles和feautre的形状保持一致。

image.png


然后这样就没有错误提示了

image.png

但是并没有显示我们画的雷达图,我们运行下面的语句显示一下雷达图

image.png

就可以看到雷达图了

0.0000
2
关注作者
收藏
评论(2)

发表评论
ermutuxia
2021-01-25

画雷达图的时候需要注意,可以用循环在里面画很多图,但是进行坐标系整体设定的时候,需要将这些设定放到循环外面,否则会进行多次设定,比如你把图例添加放到循环里面,就会添加很多次图例,导致图例重复。

0.0000 0 0 回复
ermutuxia
2021-01-25

image.png

0.0000 0 0 回复