Python繪制雷達(dá)圖時遇到的坑的解決
ValueError: The number of FixedLocator locations (9), usually from a call to set_ticks, does not match the number of ticklabels (8).
運(yùn)行書中例題時發(fā)現(xiàn)了這個錯誤,
原代碼如上:
import numpy as np import matplotlib.pyplot as plt import matplotlib matplotlib.rcParams['font.family']='simhei' # 某學(xué)生的課程與成績 courses = np.array(['C++','Python','高數(shù)','大學(xué)英語','軟件工程', '組成原理','數(shù)字圖像處理','計算機(jī)圖形學(xué)']) scores=np.array([80,95,78,85,45,65,80,60]) datalength = len(scores) #數(shù)據(jù)長度 # angles數(shù)組把圓周等分為dataLength份 angles = np.linspace(0, #數(shù)組第一個數(shù)據(jù) 2*np.pi, #數(shù)組最后一個暑假 datalength, #數(shù)組中的數(shù)據(jù)量 endpoint=False) #不包含終點(diǎn) scores = np.concatenate((scores,[scores[0]])) angles = np.concatenate((angles,[angles[0]])) # 繪制雷達(dá)圖 print(angles) print('='*20) print(scores) plt.polar(angles, scores, 'rv--', linewidth=2) # 設(shè)置角度網(wǎng)絡(luò)標(biāo)簽 plt.thetagrids(angles*180/np.pi,courses) # 填充雷達(dá)圖內(nèi)部 plt.fill(angles,scores,facecolor='r',alpha=0.6) plt.show()
自己搜查資料后,了解到,只對socres,和angles做閉合還是不夠
需要再對courses進(jìn)行閉合
courses = np.concatenate((courses,[courses[0]]))
此時就能正確運(yùn)行出來。
這里在附上霍蘭德人格分析代碼:
import numpy as np import matplotlib.pyplot as plt import matplotlib matplotlib.rcParams['font.family']="SimHei" radar_labels = np.array(['研究型(I)','藝術(shù)型(A)','社會型(S)',\ '企業(yè)型(E)','常規(guī)型(C)','現(xiàn)實(shí)型(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 =('藝術(shù)家','實(shí)驗(yàn)員','工程師','推銷員','社會工作者','記事員') 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()
到此這篇關(guān)于Python繪制雷達(dá)圖時遇到的坑的解決的文章就介紹到這了,更多相關(guān)Python繪制雷達(dá)圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python對json的相關(guān)操作實(shí)例詳解
這篇文章主要介紹了python對json的相關(guān)操作,結(jié)合實(shí)例形式詳細(xì)分析了json的概念、功能以及Python針對json的解析、輸出、排序、轉(zhuǎn)換等操作技巧,需要的朋友可以參考下2017-01-01Python數(shù)據(jù)庫編程之pymysql詳解
本文主要介紹了Python數(shù)據(jù)庫編程中pymysql,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05使用numpy實(shí)現(xiàn)topk函數(shù)操作(并排序)
這篇文章主要介紹了使用numpy實(shí)現(xiàn)topk函數(shù)操作(并排序),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05關(guān)于Pyinstaller打包eel和pygame需要注意的坑
這篇文章主要介紹了關(guān)于Pyinstaller打包eel和pygame需要注意的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02Python獲取多進(jìn)程執(zhí)行的返回值實(shí)現(xiàn)
本文主要介紹了Python獲取多進(jìn)程執(zhí)行的返回值實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03