微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

雷达/蜘蛛图的面网格

如何解决雷达/蜘蛛图的面网格

我真的希望有人能回答这个问题。我从去年开始就在做这件事,但我只能在 Excel 中完成部分工作。我怀疑我的表格格式不是最佳的,所以请随时在各个方面启发我。

这是数据和我想要实现的目标的摘要。我正在使用人口普查数据。如下所示,我收集了人口普查年份、户主生活的背景、亲属类别及其在各种背景下的分布。换句话说,我想将头部的上下文与其亲属的上下文进行比较。 data sample

如您所见,数据有点“嵌套”。我从“上下文”中挑选了 1891 年的人头,并想分析他们的亲属在各种上下文中的分布。我想在 4 个人口普查年中这样做。

至于现在,我只能设法单独完成并将它们组合在 Excel grid in Excel 中。缺点是我没有 x 和 y 轴应该分别是年份和头部的上下文。

我在 R 中尝试使用 ggradar,但不支持 facet grid(或 wrap)函数。我现在正在尝试使用 make_spider,但我缺乏 Python 知识(我正在学习 Udemy 的介绍课程,所以我并不懒惰!)大大减慢了我的速度。

我真的希望我能用 Python 来做。你认为这可能吗?我是否遗漏了一些非常明显的东西?

我非常感谢你。

在 Python 中使用该代码

    # Libraries
import openpyxl
import matplotlib.pyplot as plt
import pandas as pd
from math import pi

# Set data

df = pd.read_excel(Context.xlsx',header=0,engine='openpyxl')


# ------- PART 1: Define a function that do a plot for one line of the dataset!


def make_spider(row,title,color):
    # number of variable
    categories = list(df)[3:]
    N = len(categories)
    print(categories)
    print(N)

    # What will be the angle of each axis in the plot? (we divide the plot / number of variables)
    angles = [n / float(N) * 2 * pi for n in range(N)]
    angles += angles[:1]

    # Initialise the spider plot
    ax = plt.subplot(5,1,row + 1,subplot_kw=dict(polar=True),sharex=True,sharey=True,figsize=(28,20))

    # If you want the first axis to be on top:
    ax.set_theta_offset(pi / 2)
    ax.set_theta_direction(-1)

    # Draw one axe per variable + add labels labels yet
    plt.xticks(angles[:-1],categories,color='grey',size=8)

    # Draw ylabels
    ax.set_rlabel_position(0)
    plt.yticks([10,20,30],["10","20","30"],color="grey",size=7)
    plt.ylim(0,40)

    # Ind1
    values = df.loc[row].drop('context').values.flatten().tolist()
    values += values[:1]
    ax.plot(angles,values,color=color,linewidth=2,linestyle='solid')
    ax.fill(angles,alpha=0.4)

    # Add a title
    plt.title(title,size=11,y=1.1)



# ------- PART 2: Apply to all individuals
# initialize the figure


my_dpi = 96
plt.figure(figsize=(1000 / my_dpi,1000 / my_dpi),dpi=my_dpi)

# Create a color palette:
my_palette = plt.cm.get_cmap("Set2",len(df.index))

# Loop to plot
for row in range(0,len(df.index)):
    make_spider(row=row,title='kin' + df['kin'][row],color=my_palette(row))
plt.show()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。