Issaki
Issaki

Reputation: 1064

Could not plot multiple horizontal bar side by side

I would like to plot multiple horizontal bar charts sharing the same y-axis. To elaborate, I have 4 dataframes, each representing a bar chart. I want to use these dataframes to plot 2 horizontal bar charts at the left and another 2 at the right. Right now, I am only able to display one horizontal bar chart at left and right. Below are my desired output, code, and error

Desired Output

data1 = {
'age': ['20-24 Years', '25-29 Years', '30-34 Years', '35-39 Years', '40-44 Years', '45-49 Years'],
'single_value': [97, 75, 35, 19, 15, 13]
}

data2 = {
'age': ['20-24 Years', '25-29 Years', '30-34 Years', '35-39 Years', '40-44 Years', '45-49 Years'],
'single_value': [98, 79, 38, 16, 15, 13]
}

data3 = {
'age': ['20-24 Years', '25-29 Years', '30-34 Years', '35-39 Years', '40-44 Years', '45-49 Years'],
'single_value': [89, 52, 22, 16, 12, 13]
}

data4 = {
'age': ['20-24 Years', '25-29 Years', '30-34 Years', '35-39 Years', '40-44 Years', '45-49 Years'],
'single_value': [95, 64, 27, 18, 15, 13]
}

df_male_1 = pd.DataFrame(data1)
df_male_2 = pd.DataFrame(data2)
df_female_1 = pd.DataFrame(data3)
df_female_2 = pd.DataFrame(data4)

fig, axes = plt.subplots(ncols=2, sharey=True, figsize=(12,12))

axes[0].barh(df_male_1['age'], df_male_1['single_value'], align='center', 
color='red', zorder=10)

axes[0].barh(df_male_2['age'], df_male_2['single_value'], align='center', 
color='blue', zorder=10)

axes[0].set(title='Age Group (Male)')

axes[1].barh(df_female_1['age'], df_female_1['single_value'], 
align='center', color='pink', zorder=10)

axes[1].barh(df_female_2['age'], df_female_2['single_value'], 
align='center', color='purple', zorder=10)

axes[1].set(title='Age Group (Female)')

axes[0].invert_xaxis()
axes[0].set(yticks=df_male_1['age'])
axes[0].yaxis.tick_right()

for ax in axes.flat:
    ax.margins(0.09)
    ax.grid(True)

fig.tight_layout()
fig.subplots_adjust(wspace=0.09)
plt.show()    

Error output Error output

Upvotes: 2

Views: 1235

Answers (1)

Sheldore
Sheldore

Reputation: 39072

The problem is that currently your bars are overlapping each other because they are center aligned by default. To get the desired figure, you have to align them at the edges. To have them adjacent to each other, you have to use negative and positive heights (horizontal width of bars). You can choose the value of height as per needs

Following is the modified code (only showing relevant part)

fig, axes = plt.subplots(ncols=2, sharey=True, figsize=(12,12))

axes[0].barh(df_male_1['age'], df_male_1['single_value'], align='edge', height=0.3, 
             color='red', zorder=10)

axes[0].barh(df_male_2['age'], df_male_2['single_value'], align='edge', height=-0.3,
             color='blue', zorder=10)

axes[0].set(title='Age Group (Male)')

axes[1].barh(df_female_1['age'], df_female_1['single_value'], align='edge',height=0.3, 
             color='pink', zorder=10)

axes[1].barh(df_female_2['age'], df_female_2['single_value'], align='edge', height=-0.3, 
             color='purple', zorder=10)

enter image description here

Upvotes: 2

Related Questions