Home > front end >  How to group already grouped bar?
How to group already grouped bar?

Time:11-05

Let's assume I have pandas dataframe looking like this:

import pandas as pd
df=pd.DataFrame({'run-1a':[0.3, 0.3, 0.4, 0.4], 'run-1b':[0.3, 0.3, 0.4, 0.5],"run-2a":[0.7, 0.9, 0.8, 0.9],"run-2b":[0.2, 0.3, 0.5, 0.5], "Person":["person1","person2","person3","person4"]})
df

and now I make a bar out of it:

import matplotlib.pyplot as plt
color_list = ['b','lightskyblue', 'g','lightgreen']
ax = df.plot(x='Person',y=['run-1a','run-1b','run-2a','run-2b'], kind='bar', color=color_list)
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.xticks(rotation="horizontal")
plt.show()

and the result looks like this:

enter image description here

However I'd like to group run-1a with run1b and run2a with run2b for each of the persons. When I googled some solutions, I found only how to make grouped bars looking like my plot above, which I don't need.

This is sketch how my plot should look like:

Is there an option how to group it like that for each person,please? Thank you very much.

CodePudding user response:

If I understood correctly, You can just add empty bar between other bars, so that You will get 4 columns, where each two bars are separated.

import pandas as pd
df=pd.DataFrame({'':[0,0,0,0],'run-1a':[0.3, 0.3, 0.4, 0.4], 'run-1b':[0.3, 0.3, 0.4, 0.5],"run-2a":[0.7, 0.9, 0.8, 0.9],"run-2b":[0.2, 0.3, 0.5, 0.5], "Person":["person1","person2","person3","person4"]})
color_list = ['b','lightskyblue','w', 'g','lightgreen']
ax = df.plot(x='Person',y=['run-1a','run-1b','','run-2a','run-2b'], kind='bar', color=color_list)
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.xticks(rotation="horizontal")
plt.show()

enter image description here

  • Related