亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

matplotlib繪制雷達圖的基本配置(萬能模板案例)

 更新時間:2022年04月13日 10:31:02   作者:王小王-123  
本文主要介紹了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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論