I have a dataset containing 50 numeric variables and 1 categorical variable (segment_hc_print, having 6 categories). I want to see the spread of each variable in each category by plotting a grid of histogram, where each row would represent a category, column would represent the variable and each cell in a grid is a histogram. I am trying the code below to generate grid for single variable :
def grid_histogram(variable, bins):
fig = plt.figure(figsize=(20,10))
fig.set_size_inches(10,10, forward = True)
fig.suptitle(variable, fontsize = 8)
plt.locator_params(numticks = 4)
for i in np.arange(0, 6, 1):
ax = plt.subplot(6,1,i+1)
ax.hist(sensor_df_print_sample_v2[sensor_df_print_sample_v2.segment_hc_print == i][variable], bins)
ax.set_title("cluster = " + str(i), fontsize = 5)
ymin, ymax = ax.get_ylim()
ax.set_yticks(np.round(np.linspace(ymin, ymax, 3), 2))
xmin, xmax = ax.get_xlim()
ax.set_xticks(np.round(np.linspace(xmin, xmax,3),2))
plt.setp(ax.get_xticklabels(), rotation = 'vertical', fontsize = 4)
fig.tight_layout()
fig.savefig(str(variable) + '_histogram.pdf')
plt.show()
And this is what I am getting : sample histogram
How do I generate a grid of such histograms, each variable stacked to the right of another ? This code below generates the ideal size of histogram I need. sample histogram