I have a pandas dataframe containing qualtitative variables. I would like to find a nice way to show how an independant variable affects a dependant one. I thought that a "subdivided pie-chart" (don't know how to call it) could be a good idea but I do not know if that exists. Given a pandas data frame like the following one:
import pandas as pd
data = {'Gender': ['m','m','m','f','m','f','m','f','f','f','f','m','m'], 'Favorite character':['Jim','Jim','Pam','Jim','Pam','Jim','Jim','Jim','Pam','Pam','Jim','Jim','Pam' ]}
where m stands for male and f for female, I would like to obtain a figure looking like this
Anyone who knows how to it? or where to look for?
Thank you very much for your help!
You can use nested pie charts for something like this:
import pandas as pd
import matplotlib.pyplot as plt
import pandas as pd
data = {'Gender': ['m','m','m','f','m','f','m','f','f','f','f','m','m'], 'Favorite character':['Jim','Jim','Pam','Jim','Pam','Jim','Jim','Jim','Pam','Pam','Jim','Jim','Pam' ]}
df = pd.DataFrame(data)
level_one = df.groupby('Gender', as_index=False).count()
level_two = df.groupby(['Gender', 'Favorite character'], as_index=False).value_counts()
fig, ax = plt.subplots()
size = 0.3
vals = np.array([[60., 32.], [37., 40.], [29., 10.]])
cmap = plt.colormaps["tab20c"]
outer_colors = cmap(np.arange(3)*4)
inner_colors = cmap([1, 2, 5, 6, 9, 10])
ax.pie(level_one['Favorite character'], radius=1, colors=outer_colors,
wedgeprops=dict(width=size, edgecolor='w'), labels=['F', 'M'])
ax.pie(level_two['count'], radius=1-size, colors=inner_colors,
wedgeprops=dict(width=size, edgecolor='w'), labels=['F-Jim', 'F-Pam', 'M-Jim', 'M-Pam'])
ax.set(aspect="equal", title='Nested Pie Plot')
plt.show()