簡體   English   中英

將一個小型雷達圖嵌入到matplotlib圖中

[英]Embed a small radarchart into matplotlib plot

現在,我可以如下創建一個雷達圖。 請注意,我使它成為一個函數只是為了讓我可以簡單地將函數更干凈地插入到更大的散點圖中。

雷達圖

def radarChart(PlayerLastName):
    playerdf = dg.loc[dg['Player Name'] == name].index.tolist()[0]
    #print(playerdf)

    labels=np.array(['SOG', 'SH', 'G', 'A'])
    stats=dg.loc[playerdf,labels].values
    #print(stats)

    # Set the angle of polar axis. 
    # And here we need to use the np.concatenate to draw a closed plot in radar chart.
    angles=np.linspace(0, 2*np.pi, len(labels), endpoint=False)
    # close the plot
    stats=np.concatenate((stats,[stats[0]]))
    angles=np.concatenate((angles,[angles[0]]))

    fig = plt.figure()
    ax = fig.add_subplot(111, polar=True)
    ax.plot(angles, stats, 'o-', linewidth=1)
    ax.fill(angles, stats, alpha=0.3)
    ax.set_thetagrids(angles * 180/np.pi, labels)
    #plt.title(PlayerLastName + ' vs. ' + namegame)
    ax.grid(True)

    return 

然后,我想將此圖放在其他地方的散點圖的右下角。 由於我的情節是圓形的, 因此這篇其他文章並沒有為我提供任何方法。 任何幫助將是巨大的!

當我打電話給radarChart('someones name')我得到

在此處輸入圖片說明

我真的不想不必先將其另存為圖像,然后再將其放入繪圖中。

我不確定您想要的輸出是什么。 您應該始終提供一個最小,完整和可驗證的示例 除此之外,我不知道為什么極坐標圖會與其他任何圖都不同以創建插圖:

import matplotlib.pyplot as plt
import numpy as np

#function for the polar plot
def radarChart(Player = "SOG", left = .3, bottom = .6, width = .2, height = .2):
    #labels and positions
    labels = np.array(['SOG', 'SH', 'G', 'A'])
    angles = np.linspace(0, 360, len(labels), endpoint = False)
    #inset position
    ax = plt.axes([left, bottom, width, height], facecolor = "lightblue", polar = True)
    #label polar chart
    ax.set_thetagrids(angles, labels)
    #polar chart title
    plt.title(Player, loc = "left")

    return ax

#main figure
x = np.linspace (-3, 1, 1000)
y = 2 * np.exp(3 - x) - 1
plt.plot(x, y)
plt.xlabel("x values")
plt.ylabel("y values")
plt.title("figure with polar insets")

#inset 1
ax = radarChart(Player = "A")
plt.scatter(x[::50], y[::50])

#inset 2
ax = radarChart(left = .6, bottom = .4, width = .2, height = .2)
plt.plot(x, y)

plt.show()

樣本輸出:
在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM