matplotlib繪制雷達圖的基本配置(萬能模板案例)
介紹
雷達圖是以從同一點開始的軸上表示的三個或更多個定量變量的二維圖表的形式顯示多變量數(shù)據(jù)的圖形方法。軸的相對位置和角度通常是無信息的。 雷達圖也稱為網(wǎng)絡圖,蜘蛛圖,星圖,蜘蛛網(wǎng)圖,不規(guī)則多邊形,極坐標圖或Kiviat圖。它相當于平行坐標圖,軸徑向排列。
應用場景
用于成績的透視,比如查看你是否偏科,知曉你的興趣偏向于哪一方面

案例一(成績雷達圖重疊)
# coding=utf-8
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] #顯示中文
plt.rcParams['axes.unicode_minus']=False #正常顯示負號
results = [
{"大學英語": 87, "高等數(shù)學": 79, "體育": 95, "計算機基礎": 92, "程序設計": 85},
{"大學英語": 80, "高等數(shù)學": 90, "體育": 91, "計算機基礎": 85, "程序設計": 88}
]
data_length = len(results[0])
# 將極坐標根據(jù)數(shù)據(jù)長度進行等分
angles = np.linspace(0, 2*np.pi, data_length, endpoint=False)
labels = [key for key in results[0].keys()]
score = [[v for v in result.values()] for result in results]
# 使雷達圖數(shù)據(jù)封閉
score_a = np.concatenate((score[0], [score[0][0]]))
score_b = np.concatenate((score[1], [score[1][0]]))
angles = np.concatenate((angles, [angles[0]]))
labels = np.concatenate((labels, [labels[0]]))
# 設置圖形的大小
fig = plt.figure(figsize=(8, 6), dpi=100)
# 新建一個子圖
ax = plt.subplot(111, polar=True)
# 繪制雷達圖
ax.plot(angles, score_a, color='g')
ax.plot(angles, score_b, color='b')
# 設置雷達圖中每一項的標簽顯示
ax.set_thetagrids(angles*180/np.pi, labels)
# 設置雷達圖的0度起始位置
ax.set_theta_zero_location('N') # E W S N SW SE NW NE
# 設置雷達圖的坐標刻度范圍
ax.set_rlim(0, 100)
# 設置雷達圖的坐標值顯示角度,相對于 y 起始角度的偏移量
ax.set_rlabel_position(270)
ax.set_title("成績對比")
plt.legend(["張三", "李四"], loc='best')
plt.show()
案例二(成績雷達圖左右圖)
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] #顯示中文
plt.rcParams['axes.unicode_minus']=False #正常顯示負號
results = [{"大學英語": 87, "高等數(shù)學": 79, "體育": 95, "計算機基礎": 92, "程序設計": 85},
{"大學英語": 80, "高等數(shù)學": 90, "體育": 91, "計算機基礎": 85, "程序設計": 88}]
data_length = len(results[0])
angles = np.linspace(0, 2*np.pi, data_length, endpoint=False)
labels = [key for key in results[0].keys()]
score = [[v for v in result.values()] for result in results]
score_a = np.concatenate((score[0], [score[0][0]])) # 將每個數(shù)組的第一個元素添加到末尾,首尾相連
score_b = np.concatenate((score[1], [score[1][0]])) # 將每個數(shù)組的第一個元素添加到末尾,首尾相連
angles = np.concatenate((angles, [angles[0]]))
labels = np.concatenate((labels, [labels[0]]))
fig = plt.figure(figsize=(10, 6), dpi=100)
fig.suptitle("成績對比")
ax1 = plt.subplot(121, polar=True)
ax2 = plt.subplot(122, polar=True)
ax, data, name = [ax1, ax2], [score_a, score_b], ["張三", "李四"]
for i in range(2): # 0:左圖 張三,1:右圖 李四
for j in np.arange(0, 100+20, 20):
ax[i].plot(angles, 6*[j], '-.', lw=0.5, color='#123456') # 畫五邊形框,lw=linewidth
for j in range(5):
ax[i].plot([angles[j], angles[j]], [0, 100], ':', lw=0.7, color='green') # 畫5條半徑線,每個角度連接圓心0和頂點100
ax[i].plot(angles, data[i], color='b') # 在極坐標下畫成績折線圖
ax[i].fill(angles, data[i],color='#B34543',alpha=0.1)
ax[i].spines['polar'].set_visible(False) # 隱藏最外圈的圓
# 隱藏圓形網(wǎng)格線
ax[i].grid(False)
for a, b in zip(angles, data[i]):
ax[i].text(a, b+5, '%.00f' % b, ha='center', va='center', fontsize=12, color='b')
ax[i].set_thetagrids(angles*180/np.pi, labels)
ax[i].set_theta_zero_location('N')
ax[i].set_rlim(0, 100)
ax[i].set_rlabel_position(0)
ax[i].set_title(name[i])
plt.show()
極坐標
import matplotlib.pyplot as plt import numpy as np plt.figure(figsize=(10,5)) # 設置畫布 ax1 = plt.subplot(121, projection='polar') # 左圖: projection='polar' 表示極坐標系 ax2 = plt.subplot(122) # 右圖: 默認是直角坐標系 x = np.linspace(0,2*np.pi,9) # 0 - 2Π 平均劃分成9個點 [0,1/4,1/2,3/4,1,5/4/,3/2,7/4,2] 0pi = 2pi y = np.random.random(9)*10 # 隨機9個值 y[-1] = y[0] # 首位相連 ax1.plot(x,y,marker='.') # 畫左圖(ax1) 極坐標 (x表示角度,y表示半徑) ax2.plot(x,y,marker='.') # 畫右圖(ax2)直角坐標 (x表示橫軸,y表示縱軸) ax1.fill(x,y,alpha=0.3) ax2.fill(x,y,alpha=0.3) plt.show()

到此這篇關于matplotlib繪制雷達圖的基本配置(萬能模板案例)的文章就介紹到這了,更多相關matplotlib 雷達圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
pytorch之torchvision.transforms圖像變換實例
今天小編就為大家分享一篇pytorch之torchvision.transforms圖像變換實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
Python+OpenCV圖像處理——實現(xiàn)直線檢測
這篇文章主要介紹了Python+OpenCV如何實現(xiàn)直線檢測,幫助大家更好的利用python處理圖片,感興趣的朋友可以了解下2020-10-10
Python中的response.text與content區(qū)別詳解
這篇文章主要介紹了Python中的response.text與content區(qū)別詳解,?從網(wǎng)絡請求下來的數(shù)據(jù),他們都是字節(jié)類型的,如果服務器不指定的話,默認編碼是"ISO-8859-1",我們使用text直接拿到的是字符串類型,沒有進行解碼操作,則會出現(xiàn)亂碼問題,需要的朋友可以參考下2023-12-12
研究Python的ORM框架中的SQLAlchemy庫的映射關系
這篇文章主要介紹了研究Python的ORM框架中的SQLAlchemy庫的映射關系,SQLAlchemy庫是一個常見的Python中操作數(shù)據(jù)庫的工具,需要的朋友可以參考下2015-04-04
對python中兩種列表元素去重函數(shù)性能的比較方法
今天小編就為大家分享一篇對python中兩種列表元素去重函數(shù)性能的比較方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06
Django連接數(shù)據(jù)庫并實現(xiàn)讀寫分離過程解析
這篇文章主要介紹了Django連接數(shù)據(jù)庫并實現(xiàn)讀寫分離過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-11-11
python3利用Socket實現(xiàn)通信的方法示例
這篇文章主要介紹了python3利用Socket實現(xiàn)通信的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-05-05

