将一个小型雷达图嵌入到matplotlib图中

时间:2018-10-27 15:29:19

标签: python matplotlib

现在,我可以如下创建一个雷达图。请注意,我只是将其设置为函数,以便可以将其更干净地插入到更大的散点图中。

  

雷达图

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 

然后我想将此图放在其他地方的散点图的右下角。 This other article无法为我提供任何方法,因为我的情节是圆形的。任何帮助都会很棒!

当我打电话给radarChart('someones name')时,我得到

enter image description here

我真的不需要不必先将其另存为图像,然后再将其放入绘图中。

1 个答案:

答案 0 :(得分:0)

我不确定您想要的输出是什么。您应该始终提供Minimal, Complete, and Verifiable example。除此之外,我不知道为什么极坐标图会与其他任何图都不同以创建插图:

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()

样本输出:
enter image description here