matplotlib新版本下的霍兰德人格分析雷达图
生活随笔
收集整理的這篇文章主要介紹了
matplotlib新版本下的霍兰德人格分析雷达图
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
matplotlib新版本下的霍蘭德人格分析雷達圖
因為matplotlib版本的跟新,有些舊的代碼就會出錯,在畫霍蘭德人格分析雷達圖圖時,會出現以下錯誤:
- ValueError: The number of FixedLocator locations (7), usually from a call to set_ticks, does not match the number of ticklabels (6).
- AttributeError: ‘Text’ object has no property ‘frac’
1.舊版本代碼(在新的matplotlib下會報錯)
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)']) #雷達標簽 nAttr = 6 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, 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,'o-', linewidth=1) plt.fill(angles,data, alpha=0.25) plt.thetagrids(angles*180/np.pi, radar_labels,frac = 1.2) 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()2.錯誤分析
- ValueError: The number of FixedLocator locations (7), usually from a call to set_ticks, does not match the number of ticklabels (6).
? 這個錯誤應該是data = np.concatenate((data, [data[0]])) angles = np.concatenate((angles, [angles[0]]))這兩句代碼造成的,因為要線條閉合,所以在后面進行了拼接,網上有些人的解決方案是注釋掉,但會出現一個問題,那就是雷達圖不閉合。正確改法是:在radar_labels中重復第一個元素。
- AttributeError: ‘Text’ object has no property ‘frac’
這個錯誤是在新版本的matplotlib中這個 ‘Text’ object移除了’frac’屬性。解決方法:去掉變量。
3.新代碼(新的matplotlib版本下)
更改:重復radar_labels中的第一個元素,去除frac = 1.2
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)','研究型(I)']) #雷達標簽 nAttr = 6 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, 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,'o-', linewidth=1) 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()結果圖:
問題解決,線條合并,沒有錯誤,完美!!!解決問題了的請給個免費的👍。
總結
以上是生活随笔為你收集整理的matplotlib新版本下的霍兰德人格分析雷达图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何进行无线网络连接设置?
- 下一篇: pandas loc和iloc区别