matplotlib制作雷達圖報錯ValueError的實現(xiàn)
在教材實例編寫雷達圖時出現(xiàn)ValueError,具體如下:
ValueError: The number of FixedLocator locations (7), usually from a call to set_ticks, does not match the number of ticklabels (6).
而原代碼如下:
import numpy as np import matplotlib.pyplot as plt import matplotlib matplotlib.rcParams['font.family']='SimHei' matplotlib.rcParams['font.sans-serif'] = ['SimHei'] labels = np.array(['綜合','KDA','發(fā)育','推進','生存','輸出']) nAttr = 6 data = np.array([7,5,6,9,8,7]) angles = np.linspace(0,2*np.pi,nAttr,endpoint=False) data = np.concatenate((data,[data[0]])) angles = np.concatenate((angles,[angles[0]])) fig = plt.figure(facecolor="white") plt.subplot(111,polar=True) plt.plot(angles,data,'bo-',color ='g',linewidth=2) plt.fill(angles,data,facecolor='g',alpha=0.25) plt.thetagrids(angles*180/np.pi,labels) plt.figtext(0.52,0.95,'DOTA能力值雷達圖',ha='center') plt.grid(True) plt.show()
原因在于對array類型data、angles進行封閉時,未對labels進行相同操作,導(dǎo)致labels內(nèi)元素個數(shù)與前兩者不相同,從而出現(xiàn)ValueError。
之前在網(wǎng)上尋找解決方案,發(fā)現(xiàn)大多數(shù)答主選著將對data、angles進行封閉的語句注釋掉,但這樣就會導(dǎo)致雷達圖不完整,缺少一條連線:

而只需對labels同樣進行封閉即可:
data = np.concatenate((data,[data[0]])) angles = np.concatenate((angles,[angles[0]])) labels=np.concatenate((labels,[labels[0]])) #對labels進行封閉
最終運行結(jié)果

到此這篇關(guān)于matplotlib制作雷達圖報錯ValueError的實現(xiàn)的文章就介紹到這了,更多相關(guān)matplotlib 雷達圖報錯 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python標準庫學(xué)習(xí)之psutil內(nèi)存詳解
本篇文章給大家介紹一個Python標準庫中的psutil模塊,它是一個跨平臺庫,下面來學(xué)習(xí)一下器常用的功能及使用方法吧,有需要的同學(xué)可以借鑒參考下2021-09-09
python開發(fā)之thread實現(xiàn)布朗運動的方法
這篇文章主要介紹了python開發(fā)之thread實現(xiàn)布朗運動的方法,實例分析了Python基于多線程實現(xiàn)繪圖的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11
Python中處理字符串之endswith()方法的使用簡介
這篇文章主要介紹了Python中處理字符串之endswith()方法的使用,是Python入門中的基礎(chǔ)知識,需要的朋友可以參考下2015-05-05

