Python绘制雷达图时遇到的坑的解决
ValueError: The number of FixedLocator locations (9), usually from a call to set_ticks, does not match the number of ticklabels (8).
运行书中例题时发现了这个错误,
原代码如上:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['font.family']='simhei'
# 某学生的课程与成绩
courses = np.array(['C++','Python','高数','大学英语','软件工程',
'组成原理','数字图像处理','计算机图形学'])
scores=np.array([80,95,78,85,45,65,80,60])
datalength = len(scores) #数据长度
# angles数组把圆周等分为dataLength份
angles = np.linspace(0, #数组第一个数据
2*np.pi, #数组最后一个暑假
datalength, #数组中的数据量
endpoint=False) #不包含终点
scores = np.concatenate((scores,[scores[0]]))
angles = np.concatenate((angles,[angles[0]]))
# 绘制雷达图
print(angles)
print('='*20)
print(scores)
plt.polar(angles,
scores,
'rv--',
linewidth=2)
# 设置角度网络标签
plt.thetagrids(angles*180/np.pi,courses)
# 填充雷达图内部
plt.fill(angles,scores,facecolor='r',alpha=0.6)
plt.show()

自己搜查资料后,了解到,只对socres,和angles做闭合还是不够
需要再对courses进行闭合
courses = np.concatenate((courses,[courses[0]]))

此时就能正确运行出来。
这里在附上霍兰德人格分析代码:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['font.family']="SimHei"
radar_labels = np.array(['研究型(I)','艺术型(A)','社会型(S)',\
'企业型(E)','常规型(C)','现实型(R)'])
data = np.array([[0.40, 0.32, 0.35, 0.30, 0.30, 0.88],
[0.85, 0.35, 0.30, 0.40, 0.40, 0.30],
[0.43, 0.89, 0.30, 0.28, 0.22, 0.30],
[0.30, 0.25, 0.48, 0.85, 0.45, 0.40],
[0.20, 0.38, 0.87, 0.45, 0.32, 0.28],
[0.34, 0.31, 0.38, 0.40, 0.92, 0.28]])
data_labels =('艺术家','实验员','工程师','推销员','社会工作者','记事员')
angles = np.linspace(0, 2*np.pi, 6, endpoint=False)
fig = plt.figure(facecolor = "white")
plt.subplot(111, polar = True)
plt.plot(angles, data,'o-',linewidth=1, alpha=0.2)
plt.fill(angles, data, alpha=0.25)
plt.thetagrids(angles*180/np.pi, radar_labels)
plt.figtext(0.52, 0.95, '霍兰德人格分析', ha='center', size=20)
legend = plt.legend(data_labels, loc = (0.94, 0.80), labelspacing = 0.1)
plt.setp(legend.get_texts(), fontsize='large')
plt.grid(True)
plt.savefig('holland_radar.jpg')
plt.show()

到此这篇关于Python绘制雷达图时遇到的坑的解决的文章就介绍到这了,更多相关Python绘制雷达图内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
解决python 3 urllib 没有 urlencode 属性的问题
今天小编就为大家分享一篇解决python 3 urllib 没有 urlencode 属性的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2019-08-08
python requests.post请求404问题及解决方法
这篇文章主要介绍了python requests.post请求404问题,这里需要根据自己实际情况来分析当前接口接收数据时使用的是什么格式,但目前一般的网站都开始采用application/jsond的数据格式,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友可以参考下2022-09-09
在Python中使用swapCase()方法转换大小写的教程
这篇文章主要介绍了在Python中使用swapCase()方法转换大小写的教程,是Python入门中的基础知识,需要的朋友可以参考下2015-05-05
PyCharm设置Ipython交互环境和宏快捷键进行数据分析图文详解
这篇文章主要介绍了PyCharm设置Ipython交互环境和宏快捷键进行数据分析图文详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-04-04
Python Matplotlib绘制箱型图(箱线图)boxplot的方法详解
箱线图(箱型图)主要作用是发现数据内部整体的分布分散情况,包括上下限、各分位数、异常值等,本文为大家整理了Matplotlib绘制箱型图的所以方法,希望对大家有所帮助2023-05-05


最新评论