Home > database >  Plotting multiple grouped bar chart in a loop
Plotting multiple grouped bar chart in a loop

Time:06-06

I have a data frame that looks something like this:

product version count_before    count_after
AdM     0      770422.0         449396.0
AdM     2      732007.0         57480.0
AdM     5      NaN  477056.0
AdM     1      1071.0   309.0
AllT    0      14.0 NaN
... ... ... ...
Zam     1      973.0    415.0
Zam     0      6682982.0    465034.0
leg     0      12741.0  5573.0
leg     2      12031.0  918.0
leg     5      NaN  8794.0

Shape (105, 3)

I would like to plot for each company a grouped barplot that shows the count_before and count_after if that is possible. I've tried plotting directly from a dataframe or using matplotlib but I couldn't make the plot easy to read.

Now, I'm thinking of plotting each product separately in subplots so that in each subplot I have essentially have the same product plotting twice, once with count_before with version 0,1,2,5 and with count_after with the same.

As an example I managed to create a lineplot in subplots a while ago for each product using some other metric

enter image description here

I'd like to create something similar but using a barplot.

enter image description here

So far I have tried using seaborn to plot it but couldn't manage it.

I've also tried creating a pivot table out of the dataset so I ended up having a df pivoted with count_before then one with count_after.

version      0  1   2
publisher           
AdM         770422.0    1071.0  732007.0
All         14.0    0.0 14.0
.........................

Any ideas on how I could achieve this?

CodePudding user response:

I managed to find a solution for this. I was inspired by the following:

As a result I adapted the code I wrote for plotting lines.

plt.figure(figsize=(9,42))
plt.subplots_adjust(hspace=0.75)
plt.suptitle("Title", fontsize=18, y=0.93)
plt.style.use('seaborn-darkgrid')

barWidth = 0.25
ncol = int(all_df.index.nunique()/2)
for i, pub in enumerate(all_df.index.unique()):
    ax = plt.subplot(ncol,2,i 1)
    # get data
    temp = all_df.loc[pub].sort_values(by='version')
    # Set position of bar on X axis
    r1 = np.arange(len(temp))
    r2 = [x   barWidth for x in r1]
    # Make the plot
    plt.bar(r1, temp['count_before'], width=barWidth, edgecolor='white', facecolor='green', label='Before Test')
    plt.bar(r2, temp['count_after'], width=barWidth, edgecolor='white', facecolor='skyblue', label='After Test')
    ax.set_title(pub, fontsize=11)
    ax.set_xticks([r   barWidth for r in range(temp.shape[0])], temp['version'])
    ax.set_xlabel("Version", fontsize=12)
legend_elements = [Patch(facecolor='green', edgecolor='white',
                         label='Before Test'),
                   Patch(facecolor='skyblue', edgecolor='white',
                         label='After Test')]
ax.legend(ncol=2, handles=legend_elements, bbox_to_anchor=(0.4, 30), fontsize=12, frameon=True)
plt.show()
  • Related