I am trying to combine two approaches at creating histograms.
#Sample Data
df = pd.DataFrame({'V1':[1,2,3,4,5,6],
'V2': [43,35,6,7,31,34],
'V3': [23,75,67,23,56,32],
'V4': [23,45,67,63,56,32],
'V5': [23,5,67,23,6,2],
'V6': [23,78,67,76,56,2],
'V7': [23,45,67,53,56,32],
'V8': [5,5,5,5,5,5],
'cat': ["A","B","C","A","B","B"],})
I am able to create a histogram matrix for each category using this code.
#1. Creating histogram matrix for each category
for i in df['cat'].unique():
fig, ax = plt.subplots()
df[df['cat']==i].hist(figsize=(20,20),ax =ax)
fig.suptitle(i + " Feature-Class Relationships", fontsize = 20)
fig.savefig('Histogram Matrix.png' %(i), dpi = 240)
This creates a separate histogram matrix for each category. However what I would like is for the categories to be overlaid on the same matrix.
I am able to create an overlaid histogram using this approach:
#2. Overlaid histrogram for single variable
fig, ax = plt.subplots()
for i in df['cat'].unique():
df[df['cat']==i]['V8'].hist(figsize=(12,8),ax =ax, alpha = 0.5, label = i)
ax.legend()
plt.show()
However this only creates a single overlaid image. I want to create an overlaid histogram for all of variables in the matrix i.e. all categories shown in the same matrix rather than a separate matrix for each category. I have created the following code, which is a combination of the above two approaches, but it does not overlay each of the histogram matrices together and only the last plot is created.
#3. Combining approaches to create a matrix of overlaid histograms
fig, ax = plt.subplots()
for i in df['cat'].unique():
df[df['cat']==i].hist(figsize=(12,8),ax =ax, alpha = 0.5, label = i)
ax.legend()
fig.savefig('Combined.png', dpi = 240)
Is what I am trying to do possible?