1.例一
圖1
代碼1

#第1步:導出模塊 import numpy as np import matplotlib.pyplot as plt from matplotlib import font_manager # 中文字體設置第1步,導出模塊 #中文字體設置第2步:引出字體模塊和位置 my_font = font_manager.FontProperties(fname="/usr/share/fonts/truetype/noto/simsun.ttf") #數據來源,單獨設定,非文件來源 #dataLenth = 8 #數據個數,8組數據 #標簽 labels = np.array(['3℃','5℃','6℃','3℃','1℃','3℃','3℃','2℃']) data = np.array([3,5,6,3,1,3,3,2]) #數據值,與上面labels有對應關系 #雷達圖的數據格式,基本固定 #angles = np.linspace(0, 2*np.pi, dataLenth, endpoint=False) #如果沒有dataLenth = 8==len(labels),也可以這樣 angles = np.linspace(0, 2*np.pi, len(labels), endpoint=False) data = np.concatenate((data, [data[0]])) angles = np.concatenate((angles, [angles[0]])) #導出fig圖片設置,雷達圖這種格式基本固定 fig = plt.figure() #polar=true,就是ax.set_thetagrids可以顯示 ax = fig.add_subplot(111, polar=True) #數據線的寬度2,ro-代表red-;bo-代表blue-;yo-代表yellow-;o-代表深藍deepblue- ax.plot(angles, data, 'ro-', linewidth=2) #ax.set_thetagrids(angles * 180/np.pi, labels, fontproperties="SimHei") #bug,本機報錯 ax.set_thetagrids(angles * 180/np.pi, labels, fontproperties=my_font) #fig標題設置,中文字體設置 #中文字體設置第3步,在顯示中文的地方,增加u和fontproperties=my_font #ax.set_title("溫度變化雷達圖", va='bottom', fontproperties="SimHei") ax.set_title(u"溫度變化雷達圖", va='bottom', fontproperties=my_font) ax.grid(True) #顯示雷達圖的一圈一圈的線,8個圈線 #顯示圖片 plt.show()
2.例二
圖2
代碼二

#導出模塊 import pandas as pd import numpy as np import matplotlib.pyplot as plt from matplotlib import font_manager # 中文字體設置第1步,導出模塊 #中文字體設置 #plt.rcParams['font.sans-serif'] = ['KaiTi'] # 顯示中文,本機不行 #中文字體設置第2步:引出字體模塊和位置 my_font = font_manager.FontProperties(fname="/usr/share/fonts/truetype/noto/simsun.ttf") #中文字體設置第3步,在顯示中文的地方,在這里增加u labels = np.array([u'李白', u'王維', u'杜甫',u'白居易']) # 這種中文字體設置很難,分2個地方 #dataLenth = 4 # 數據長度 data_radar = np.array([63, 1, 15, 13]) # 數據 #angles = np.linspace(0, 2*np.pi, dataLenth, endpoint=False) # 分割圓周長 #如果不設置dataLenth = 4,也可以len(labels)=4,代表4組數據 angles = np.linspace(0, 2*np.pi, len(labels), endpoint=False) # 分割圓周長 data_radar = np.concatenate((data_radar, [data_radar[0]])) # 閉合 angles = np.concatenate((angles, [angles[0]])) # 閉合 plt.polar(angles, data_radar, 'bo-', linewidth=1) # 做極坐標系 # 中文設置的labels,在這里加fontproperties=my_font plt.thetagrids(angles * 180/np.pi, labels,fontproperties=my_font) plt.fill(angles, data_radar, facecolor='r', alpha=0.25)# 填充 plt.ylim(0, 70) #中文字體設置第3步,在顯示中文的地方,增加u和fontproperties=my_font #va='bottom',默認這個;top可能與王維重疊 plt.title(u'四個人的年總收入', fontproperties=my_font) #標題設置 plt.show()
3.例三
3.1 讀取csv數據
L R F M C
customer0 -0.18851 0.701298 -0.66178 -0.68228 -0.43681
customer1 0.050142 -0.33906 0.092392 0.065064 0.09756
customer2 -0.05403 -0.03271 -0.30585 -0.33106 0.011589
customer3 0.309113 -0.64527 1.378002 1.4691 0.3989
customer4 -0.12259 0.332883 -0.53543 -0.54537 -0.08043
3.2 圖3
3.3 代碼3

#導出模塊 import pandas as pd import numpy as np import matplotlib.pyplot as plt #定義 def result_pic(result): # 解析出類別標簽和種類 labels = ['L', 'R', 'F', 'M', 'C'] kinds = list(result.iloc[:, 0]) # 由於在雷達圖中,要保證數據閉合,這里就再添加L列,並轉換為 np.ndarray result = pd.concat([result, result[['L']]], axis=1) centers = np.array(result.iloc[:, 1:]) angle = np.linspace(0, 2 * np.pi, len(labels), endpoint=False) angle = np.concatenate((angle, [angle[0]])) # 繪圖 fig = plt.figure() ax = fig.add_subplot(111, polar=True) # 參數polar, 以極坐標的形式繪制圖形 # 畫線 for i in range(len(kinds)): ax.plot(angle, centers[i], linewidth=2, label=kinds[i]) # 添加屬性標簽 ax.set_thetagrids(angle * 180 / np.pi, labels) plt.title('data show') plt.legend(loc='lower right') plt.show() if __name__ == '__main__': result = pd.read_csv('data2.csv', sep=',') result_pic(result) #注意細節:一般我們都是喜歡用excel或者wps表格進行數據的輸入 #但是在保存數據時,喜歡直接更改文件的屬性,導致數據讀不出來 #應該是將文件另存為.csv格式