0

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

1 Answer 1

1

if I understand correctly, you could just create a grid with plt.subplots(). In the example below, I am plotting the first 5 variables as columns:

nr_of_categories = 6
nr_of_variables = 5

fig, ax = plt.subplots(nrows = nr_of_categories, cols = nr_of_variables, figsize = (20, 20))

for category in np.arange(0, nr_of_categories):
  for variable in np.arange(0, nr_of_variables):
    ax[category, variable].hist(sensor_df_print_sample_v2[sensor_df_print_sample_v2.segment_hc_print == i][variable], bins)

    # and then the rest of your code where you replace ax with ax[category, variable]


Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.