How to print a histogram for each variation of two categorical variables

I have a pandas data frame that contains three columns, [Partner,Market,Integer]. I'm trying to create a frequency distribution histogram of the Integer value for each of the Partner/Market variations. There are more than 10 partners and Markets.

I've been tinkering and tweaking and the closest I was able to get was the below code. This gives me exactly what I want in terms of the graph but does not account for the Market value. I could use by='Market' but then it creates these small unreadable boxes. Is there a way for it to show each graph beneath another one so that the width and height are unaffected?

ax = df_trimmed[df_trimmed['Partner'] == 'Partner A'].hist(column='Integer', bins=len(df_trimmed['Integer'].unique()),grid=False, figsize=(12,8), color='#86bf91')

plt.xticks(np.arange(0, 901, 30))

Another way I could do this is to add & df_trimmed['Market'] == 'Market A' to df_trimmed[df_trimmed['Partner'] == 'Partner A'] and create a for loop that goes through each market in each partner, but I'm thinking there must be an easier solution. Edit: I actually did it and it does bring me closer to my end goal.

for partner in df_trimmed['Partner'].unique():
    for market in df_trimmed['Market'].unique():

        ax = df_trimmed[(df_trimmed['Partner']==partner) & (df_trimmed['Market']==market)].hist(column='Integer', bins=len(df_trimmed['Integer'].unique()),grid=False, figsize=(12,8), color='#86bf91')

        plt.xticks(np.arange(0, 901, 30))
        
        plt.title(f'{partner} - {market}')

        plt.show()

My end goal is to have a PDF for each partner containing freq. distribution of the Integer column for each market. I'm not really sure how to do that. Also, If there was a way of creating a line graph instead of a histogram, that would also be amazing (can't figure out how to do that). The x asis represents seconds, so maybe I could create a custom x axis and then somehow plot counts of the integer values on there?



Read more here: https://stackoverflow.com/questions/68492628/how-to-print-a-histogram-for-each-variation-of-two-categorical-variables

Content Attribution

This content was originally published by olaf007 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: