简体   繁体   English

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

[英]Embed a small radarchart into matplotlib plot

Right now I can create a radarchart as follows. 现在,我可以如下创建一个雷达图。 Note that I made it a function just so that I can simply insert the function into my larger scatterplot more cleanly. 请注意,我使它成为一个函数只是为了让我可以简单地将函数更干净地插入到更大的散点图中。

Radar Chart 雷达图

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 

I then want to put this figure in the bottom right of my scatter plot I have elsewhere. 然后,我想将此图放在其他地方的散点图的右下角。 This other article does not provide me with any way to do this since my plot is circular. 由于我的情节是圆形的, 因此这篇其他文章并没有为我提供任何方法。 Any help would be great! 任何帮助将是巨大的!

When I call radarChart('someones name') I get 当我打电话给radarChart('someones name')我得到

在此处输入图片说明

I would really like to not have to save it as an image first and then put it in the plot. 我真的不想不必先将其另存为图像,然后再将其放入绘图中。

I am not sure, what your desired output is. 我不确定您想要的输出是什么。 You should always provide a Minimal, Complete, and Verifiable example . 您应该始终提供一个最小,完整和可验证的示例 Apart from this, I don't know, why a polar plot would be different from any other plot to create an inset: 除此之外,我不知道为什么极坐标图会与其他任何图都不同以创建插图:

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

Sample output: 样本输出:
在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM