I am trying to display a grouped bar chart by categorical values on columns.
An example data is below.
df = pd.DataFrame({'Type': ['A','A','A','A','A','A','A','A','A','B','B','B','B','B','B','B','B','B'],
'Ratio': [3, 3, 3, 5, 5, 5, 7, 7, 7,3, 3, 3, 5, 5, 5, 7, 7, 7],
'Method': ['X','Y','Z','X','Y','Z','X','Y','Z','X','Y','Z','X','Y','Z','X','Y','Z'],
'Result': [90, 85, 96, 89, 82, 80, 78, 72, 75, 91, 82, 94, 87, 86, 84, 71, 78, 86]})
Values in "Type" column can be on the same chart or two subplot graphs. The Y axis must display the value in "Result" and the legend of the bar chart must display the value in "Method"
CodePudding user response:
This should work multiple types in your Type column creating vertical subplots for each.
item_list = list(df.Type.unique())
fig, ax = plt.subplots(len(item_list), figsize=(12, 8))
for t in range(len(item_list)):
df1 = df.loc[df["Type"] == item_list[t]]
for r, w in zip([3,5,7], [5, 10, 15]):
df2 = df1.loc[df1["Ratio"] == r]
for m, i in zip(["X","Y","Z"], range(3)):
df3 = df2.loc[df2["Method"] == m]
label = str(m)
ax[t].bar((w i), df3["Result"], label=label)
ax[t].set_title(f'Type{item_list[t]}')