1

I really wish someone will answer to that. I'm working on this since last year and I only managed to partly do it in Excel. I suspect that my table format is not optimal for that so feel free to enlighten me on every aspect.

Here's a summary of the data and of what I want to achieve. I'm using Census data. As shown next, I gather the census year, the context in which the household head(s) is living, kin categories and their distribution in various contexts. In other words, I want to compare the head's context to the its kin's. data sample

As you can see, the data is bit "nested". I pick heads from 1891 coming from "contexts" and want to analyze the distribution of their kin in various contexts. I want to do that for 4 census years.

As for now I only managed to do it separately and assemble them in Excel grid in Excel. The drawback is that I do not have both x and y axis that should be respectively years and head's contexts.

I tried in R with ggradar but the facet grid (or wrap) function was not supported. I'm trying now with make_spider but my lack of knowledge in Python (I'm following an introduction course at Udemy, so I'm not lazy!) is considerably slowing me down.

I really hope that I can do it in Python. Do you think it's possible? Am I missing something really obvious?

I could not thank you more.

Using that code in 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, values, color=color, alpha=0.4)

    # Add a title
    plt.title(title, size=11, color=color, 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()
Jean-Seb
  • 11
  • 2
  • can you try to provide the data not in a screenshot? https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – StupidWolf Jan 30 '21 at 18:28
  • @StupidWolf, it seems that I can only upload images to this post. I tried from my table in Excel, Notepad and Word and the paste function convert everything to an image. What am I missing here? – Jean-Seb Jan 30 '21 at 18:48

0 Answers0